zoukankan      html  css  js  c++  java
  • 对图片的处理

      上周因为忙这发布应用所以没用进行总结,这周准备把上周的补上。

      先说明一下,因为是每周遇到的问题总结,所以问题与问题本身可能没有联系,也没有连贯性,大家看起跳跃性会很大,还有就是大神们就请绕过吧!

      1.图片等比例压缩

      

    等比例压缩
    -(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
        UIImage *newImage = nil;
        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
        CGFloat targetWidth = size.width;
        CGFloat targetHeight = size.height;
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
        if(CGSizeEqualToSize(imageSize, size) == NO){
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
            if(widthFactor > heightFactor){
                scaleFactor = widthFactor;
            }
            else{
                scaleFactor = heightFactor;
            }
            scaledWidth = width * scaleFactor;
            scaledHeight = height * scaleFactor;
            if(widthFactor > heightFactor){
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            }else if(widthFactor < heightFactor){
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }
    
        UIGraphicsBeginImageContext(size);
    
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width = scaledWidth;
        thumbnailRect.size.height = scaledHeight;
        [sourceImage drawInRect:thumbnailRect];
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        if(newImage == nil){
            NSLog(@"scale image fail");
        }
    
        UIGraphicsEndImageContext();
        
        return newImage;
        
    }
    
    -(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
        UIImage *newImage = nil;
        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
        CGFloat targetWidth = defineWidth;
        CGFloat targetHeight = height / (width / targetWidth);
        CGSize size = CGSizeMake(targetWidth, targetHeight);
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
        if(CGSizeEqualToSize(imageSize, size) == NO){
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
            if(widthFactor > heightFactor){
                scaleFactor = widthFactor;
            }
            else{
                scaleFactor = heightFactor;
            }
            scaledWidth = width * scaleFactor;
            scaledHeight = height * scaleFactor;
            if(widthFactor > heightFactor){
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            }else if(widthFactor < heightFactor){
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }
        UIGraphicsBeginImageContext(size);
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width = scaledWidth;
        thumbnailRect.size.height = scaledHeight;
    
        [sourceImage drawInRect:thumbnailRect];
    
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        if(newImage == nil){
            NSLog(@"scale image fail");
        }
    
        UIGraphicsEndImageContext();
        return newImage;
    }

    2.按宽度压缩

    -(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
        UIImage *newImage = nil;
        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
        CGFloat targetWidth = defineWidth;
        CGFloat targetHeight = height / (width / targetWidth);
        CGSize size = CGSizeMake(targetWidth, targetHeight);
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
        if(CGSizeEqualToSize(imageSize, size) == NO){
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
            if(widthFactor > heightFactor){
                scaleFactor = widthFactor;
            }
            else{
                scaleFactor = heightFactor;
            }
            scaledWidth = width * scaleFactor;
            scaledHeight = height * scaleFactor;
    //center
    if(widthFactor > heightFactor){ thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; }else if(widthFactor < heightFactor){ thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(size); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil){ NSLog(@"scale image fail"); } UIGraphicsEndImageContext(); return newImage; }
  • 相关阅读:
    IIS支持apk文件下载
    【转】Winform输入法控制
    WebRequest请求Url中文乱码
    c#实现Form窗体始终在桌面最前端显示
    在静态页面中使用 Vue.js
    NPOI 的使用姿势
    C# 模拟 HTTP POST请求
    WinForm 绑定到嵌套对象上的属性
    彻底清除 Windows 服务
    WPF DataGrid 绑定行双击行命令
  • 原文地址:https://www.cnblogs.com/lxgo/p/5080293.html
Copyright © 2011-2022 走看看