首页 > 代码库 > IOS 按比例裁剪图片

IOS 按比例裁剪图片

拍照或者从图片库中获取图片 操作过程中容易闪退,也总会有内存压力警告,第一步,首先可以考虑裁剪图片,实际上可能不需要那么大的。其次可以考虑把耗时的比如存储过程放进线程。

这里封装裁剪图片的类方法。

//NavView.m

  #define IMAGE_MAX_SIZE_WIDTH 640  

  #define IMAGE_MAX_SIZE_GEIGHT 1136

+(UIImage *)fitSmallImage:(UIImage *)image{    if (nil == image)    {        return nil;    }    if (image.size.width<IMAGE_MAX_SIZE_WIDTH && image.size.height<IMAGE_MAX_SIZE_GEIGHT)    {        return image;    }    CGSize size = [self fitsize:image.size];    UIGraphicsBeginImageContext(size);    CGRect rect = CGRectMake(0, 0, size.width, size.height);    [image drawInRect:rect];    UIImage *newing = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return newing;}+ (CGSize)fitsize:(CGSize)thisSize{    if(thisSize.width == 0 && thisSize.height ==0)        return CGSizeMake(0, 0);    CGFloat wscale = thisSize.width/IMAGE_MAX_SIZE_WIDTH;    CGFloat hscale = thisSize.height/IMAGE_MAX_SIZE_GEIGHT;    CGFloat scale = (wscale>hscale)?wscale:hscale;    CGSize newSize = CGSizeMake(thisSize.width/scale, thisSize.height/scale);    return newSize;}
使用eg:
把要操作的图片设为image(uiimage)
image=[NavView
fitSmallImage:image];