美文网首页
SDWebImage 加载高分辨率图会引起内存暴增 or APP

SDWebImage 加载高分辨率图会引起内存暴增 or APP

作者: luckycoder | 来源:发表于2017-12-05 15:52 被阅读0次

    1.增加压缩图片方法

    + (UIImage *)compressImage:(UIImage *)image
    {
        float imageWidth = image.size.width;
        float imageHeight = image.size.height;
        float width = 640;
        float height = image.size.height/(image.size.width/width);
        
        float widthScale = imageWidth /width;
        float heightScale = imageHeight /height;
    
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        
        if (widthScale > heightScale) {
            [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
        }
        else {
            [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
        }
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return newImage;
        
    }
    

    2.修改 sd_imageWithData 方法

    + (UIImage *)sd_imageWithData:(NSData *)data {
        if (!data) {
            return nil;
        }
        
        UIImage *image;
        NSString *imageContentType = [NSData sd_contentTypeForImageData:data];
        if ([imageContentType isEqualToString:@"image/gif"]) {
            image = [UIImage sd_animatedGIFWithData:data];
        }
    #ifdef SD_WEBP
        else if ([imageContentType isEqualToString:@"image/webp"])
        {
            image = [UIImage sd_imageWithWebPData:data];
        }
    #endif
        else {
            image = [[UIImage alloc] initWithData:data];
            if (data.length/1024 > 128) {
                image = [self compressImageWith:image];
                data =UIImageJPEGRepresentation(image,1.0);
            }
            UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];
            if (orientation != UIImageOrientationUp) {
                image = [UIImage imageWithCGImage:image.CGImage
                                            scale:image.scale
                                      orientation:UIImageOrientationUp];
            }
        }
    
    
        return image;
    }
    

    相关文章

      网友评论

          本文标题:SDWebImage 加载高分辨率图会引起内存暴增 or APP

          本文链接:https://www.haomeiwen.com/subject/yxecixtx.html