第七篇、OC_圖片的裁剪基於SDWebImage

來源:互聯網
上載者:User

標籤:抓取   記憶體緩衝   自動   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.