壓縮圖片 減小app內存消耗

//壓縮圖片  解決圖片過多  內存過大圖片

+ (UIImage *)newImageWithImage:(UIImage *)image{內存

    //實現等比例縮放it

    float hfactor = image.size.width/KWidth;io

    float vfactor = image.size.height/KHeight;float

    float factor = fmaxf(hfactor, vfactor);im

    

    //畫布大小new

    CGFloat newWidth = image.size.width/factor;壓縮

    CGFloat newHeight = image.size.height/factor;return

    CGSize newSize = CGSizeMake(newWidth, newHeight);

    

    UIGraphicsBeginImageContext(newSize);

    [image drawInRect:SetFrame(0, 0, newWidth, newHeight)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    //圖像壓縮

    NSData *newImageData = UIImageJPEGRepresentation(newImage, 0.5);

    

    return [UIImage imageWithData:newImageData];

}

相關文章
相關標籤/搜索