荼菜的ios笔记–图片裁剪黑魔法。

LiangShi.gif
前言:最近在做一款对话交互式的美食机器人应用,目前内测阶段,效果如上图。最近闲暇之余会把期间总结的一些知识点写下来,做个总结。前期有段时间困扰了我很久一个问题由于工程中的图片数据抓取自不同平台,所以图片的大小尺寸不一定,而放置图片的imageView尺寸是一定的,不作任何处理的话会导致图片拉伸变形,因此找了好久解决办法,现把它拿出来。

*图片裁剪代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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 === %fnimageHeight === %fnImageView宽高比 == %fnimageScale == %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

后话:附上之前写的文章,欢迎指正:

荼菜的iOS笔记–Core Animation 核心动画

荼菜的iOS笔记–UIView的几个Block动画
荼菜的iOS笔记–一些实用功能。
荼菜的iOS笔记–UITableViewCell的各种操作(刷新、插入、删除、动画)
荼菜的iOS笔记–一张图记住所有git命令行操作。
荼菜的iOS笔记–一张图告诉你程序员需要知道的这些网站。
荼菜的iOS笔记–iOS自动打包脚本(Python)

荼菜的iOS笔记–我的编码规范参考。
荼菜的iOS笔记–iOS基础优秀博客总结ToDoList
荼菜的iOS笔记–Xcode Tips
当然,我还写过诗。。。
光。