图片加载 内存优化

图片加载内存优化

UIImage内存占用大小:image.size.width * image.size.height * image.scale

优化方向:缩小图片的尺寸大小

/*** 缩放图片至新尺寸 */
+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGFloat)size {
    
    CGFloat imgW = image.size.width;
    CGFloat imgH = image.size.height;
    
    if (imgW > size || imgH > size) {
        
        CGFloat sizeW = 0.0, sizeH = 0.0;
        
        if (imgW > imgH) {
            sizeH = size;
            sizeW = imgW / imgH * size;
        } else {
            sizeW = size;
            sizeH = imgH / imgW * size;
        }
        
        UIGraphicsBeginImageContext(CGSizeMake(sizeW, sizeH));
        [image drawInRect:CGRectMake(0, 0, sizeW, sizeH)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    } else {
        return image;
    }
}

你可能感兴趣的:(图片加载 内存优化)