博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 图片处理( 1.按比例缩放 2.指定宽度按比例缩放
阅读量:6826 次
发布时间:2019-06-26

本文共 2807 字,大约阅读时间需要 9 分钟。

本文转载至 http://blog.sina.com.cn/s/blog_6f29e81f0101tat6.html

 

//按比例缩放,size 是你要把图显示到 多大区域 CGSizeMake(300, 140)

-(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;

}

你可能感兴趣的文章
根据Web服务器记录来追击黑客
查看>>
Java类和对象的初始化顺序
查看>>
Oracle 11g RAC 二节点root.sh执行报错故障一例
查看>>
ocs的部署与应用(一)
查看>>
Python黑客编程之信息收集
查看>>
Django+ PowerShell 管理AD系统
查看>>
leopard ich7 alc 888 driver
查看>>
开始学习Docker啦--容器理论知识(一)
查看>>
流数据库 概率计算概念 - PipelineDB-Probabilistic Data Structures & Algorithms
查看>>
Java注解不完全总结
查看>>
解决“此电脑上没有安装True Speech声音解码器”的方法
查看>>
Win08R2变脸Win7第一招配置Owner信息
查看>>
远程桌面连接 详细图解
查看>>
Linux下查看文件和文件夹大小
查看>>
redis总结
查看>>
CsGL着色的三角形
查看>>
后端码农谈前端(CSS篇)第七课:定位与浮动
查看>>
springboot(十八):使用Spring Boot集成FastDFS
查看>>
何勉:第一性原理和精益敏捷的规模化实施
查看>>
HDFS 文件格式——SequenceFile RCFile
查看>>