標籤:抓取 記憶體緩衝 自動 shared ret create color return imageview
前期有段時間困擾了我很久一個問題由於工程中的圖片資料抓取自不同平台,所以圖片的大小尺寸不一定,而放置圖片的imageView尺寸是一定的,不作任何處理的話會導致圖片展開變形,因此找了好久解決辦法,現把它拿出來。
#import <UIKit/UIKit.h>#import "UIImageView+WebCache.h"@interface UIImageView (WebImage)/** * @author Tucai, 16-02-23 12:02:53 * * 設定能夠自動裁剪的網狀圖,基於SDWebImage實現 * */// 模糊圖渲染- (void)renderBlurredImageWithUrl:(NSString *)url placeholder:(UIImage *)placeholder completed:(imageDownloadCompletedBlock) completedBlock;//按比例縮放網狀圖片- (void)yg_setTrimImageWithUrl:(NSString *)url placeholderImage:(UIImage *)placeholder;
#import "UIImageView+WebImage.h"#import "NSString+URLEncoding.h"@implementation UIImageView (WebImage)#pragma mark - 模糊圖渲染- (void)renderBlurredImageWithUrl:(NSString *)url placeholder:(UIImage *)placeholder completed:(imageDownloadCompletedBlock) completedBlock{ // 這裡必須開啟記憶體緩衝 [SDWebImageManager sharedManager].imageCache.shouldCacheImagesInMemory = YES; // 渲染背景 __weak typeof(self) ws = self; [ws sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *webImage, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { // 999 是一個標記 if (ws.tag != 999) { UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithFrame:ws.bounds]; UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; visualView.effect = effect; NSLog(@"only once"); [ws addSubview:visualView]; ws.tag = 999; } ws.alpha =0.6; ws.image = nil; ws.image = webImage; if (completedBlock) { completedBlock(webImage); } }];}#pragma mark - 裁剪圖片- (void)yg_setTrimImageWithUrl:(NSString *)url placeholderImage:(UIImage *)placeholder{ __weak typeof(self) ws = self; [SDWebImageManager sharedManager].imageCache.shouldCacheImagesInMemory = NO; [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if (image) { UIImage *img=[self yg_trimImageWithImage:image]; ws.image=img; }else{ ws.image =[self yg_trimImageWithImage:placeholder]; } }];}-(UIImage *)yg_trimImageWithImage:(UIImage *)image{ //imageView的寬高比 CGFloat imageViewWidthHeightRatio =self.frame.size.width/self.frame.size.height; //螢幕解析度// CGFloat imageScale = [[UIScreen mainScreen] scale]; CGFloat imageScale = 1; CGFloat imageWith = image.size.width*imageScale; CGFloat imageHeight =image.size.height*imageScale; //image的寬高比 CGFloat imageWidthHeightRatio =imageWith/imageHeight; CGImageRef imageRef = nil; CGRect rect;// NSLog(@"\nimageWith === %f\nimageHeight === %f\nImageView寬高比 == %f\nimageScale == %f",imageWith,imageHeight,imageViewWidthHeightRatio,imageScale); if (imageWidthHeightRatio>imageViewWidthHeightRatio) { rect = CGRectMake((imageWith-imageHeight*imageViewWidthHeightRatio)/2, 0, imageHeight*imageViewWidthHeightRatio, imageHeight); }else if (imageWidthHeightRatio<imageViewWidthHeightRatio) { rect = CGRectMake(0, (imageHeight-imageWith/imageViewWidthHeightRatio)/2, imageWith, imageWith/imageViewWidthHeightRatio); }else { rect = CGRectMake(0, 0, imageWith, imageHeight); } imageRef = CGImageCreateWithImageInRect([image CGImage], rect); UIImage *res = [UIImage imageWithCGImage:imageRef scale:imageScale orientation:UIImageOrientationUp]; /** 一定要,千萬要release,否則等著記憶體泄露吧,稍微高清點的圖一張圖就是幾M記憶體,很快App就掛了 */ CGImageRelease(imageRef); return res;}@end
第七篇、OC_圖片的裁剪基於SDWebImage