ios 图片处理( 1.按比例缩放 2.指定宽度按比例缩放)

点击可查看我所写内容所对应的项目:https://github.com/liyuunxiangGit/CircleOfFriendsDisplay(一个朋友圈的实现)

首先我这里只拿按比例缩放进行讲解。大家可以先看下方的两个效果图:

                             之前的效果图  图片变形

                             修改后的  没有变形


代码在此:

  
  
  1. //按比例缩放,size 是你要把图显示到 多大区域 CGSizeMake(300, 140)
  2. -(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
  3.    UIImage *newImage = nil;
  4.    CGSize imageSize = sourceImage.size;
  5.    CGFloat width = imageSize.width;
  6.    CGFloat height = imageSize.height;
  7.    CGFloat targetWidth = size.width;
  8.    CGFloat targetHeight = size.height;
  9.    CGFloat scaleFactor = 0.0;
  10.    CGFloat scaledWidth = targetWidth;
  11.    CGFloat scaledHeight = targetHeight;
  12.    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
  13.    if(CGSizeEqualToSize(imageSize, size) == NO){
  14.        CGFloat widthFactor = targetWidth / width;
  15.        CGFloat heightFactor = targetHeight / height;
  16.        if(widthFactor > heightFactor){
  17.            scaleFactor = widthFactor;
  18.        }
  19.        else{
  20.            scaleFactor = heightFactor;
  21.        }
  22.        scaledWidth = width * scaleFactor;
  23.        scaledHeight = height * scaleFactor;
  24.        if(widthFactor > heightFactor){
  25.            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  26.        }else if(widthFactor < heightFactor){
  27.            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  28.        }
  29.    }
  30.    UIGraphicsBeginImageContext(size);
  31.    CGRect thumbnailRect = CGRectZero;
  32.    thumbnailRect.origin = thumbnailPoint;
  33.    thumbnailRect.size.width = scaledWidth;
  34.    thumbnailRect.size.height = scaledHeight;
  35.    [sourceImage drawInRect:thumbnailRect];
  36.    newImage = UIGraphicsGetImageFromCurrentImageContext();
  37.    if(newImage == nil){
  38.        NSLog(@"scale image fail");
  39.    }
  40.    UIGraphicsEndImageContext();
  41.    return newImage;
  42. }
  43. //指定宽度按比例缩放
  44. -(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
  45.    UIImage *newImage = nil;
  46.    CGSize imageSize = sourceImage.size;
  47.    CGFloat width = imageSize.width;
  48.    CGFloat height = imageSize.height;
  49.    CGFloat targetWidth = defineWidth;
  50.    CGFloat targetHeight = height / (width / targetWidth);
  51.    CGSize size = CGSizeMake(targetWidth, targetHeight);
  52.    CGFloat scaleFactor = 0.0;
  53.    CGFloat scaledWidth = targetWidth;
  54.    CGFloat scaledHeight = targetHeight;
  55.    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
  56.    if(CGSizeEqualToSize(imageSize, size) == NO){
  57.        CGFloat widthFactor = targetWidth / width;
  58.        CGFloat heightFactor = targetHeight / height;
  59.        if(widthFactor > heightFactor){
  60.            scaleFactor = widthFactor;
  61.        }
  62.        else{
  63.            scaleFactor = heightFactor;
  64.        }
  65.        scaledWidth = width * scaleFactor;
  66.        scaledHeight = height * scaleFactor;
  67.        if(widthFactor > heightFactor){
  68.            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  69.        }else if(widthFactor < heightFactor){
  70.            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  71.        }
  72.    }
  73.    UIGraphicsBeginImageContext(size);
  74.    CGRect thumbnailRect = CGRectZero;
  75.    thumbnailRect.origin = thumbnailPoint;
  76.    thumbnailRect.size.width = scaledWidth;
  77.    thumbnailRect.size.height = scaledHeight;
  78.    [sourceImage drawInRect:thumbnailRect];
  79.    newImage = UIGraphicsGetImageFromCurrentImageContext();
  80.    if(newImage == nil){
  81.        NSLog(@"scale image fail");
  82.    }
  83.    UIGraphicsEndImageContext();
  84.    return newImage;
  85. }

1



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值