转自:http://www.jianshu.com/p/f9fbc54b0fa9
//返回裁剪区域图片,返回还是原图大小,除图片以外区域清空
- (UIImage*)clipWithImageRect:(CGRect)imageRect clipRect:(CGRect)clipRectclipImage:(UIImage *)clipImage;
{
// 开启位图上下文
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0);
// 设置裁剪区域
UIBezierPath *path = [UIBezierPath bezierPathWithRect:clipRect];
[path addClip];
// 绘制图片
[clipImage drawInRect:clipRect];
// 获取当前图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return image;
}
//返回裁剪区域图片,返回裁剪区域大小图片
- (UIImage *)clipWithImageRect:(CGRect)clipRectclipImage:(UIImage *)clipImage;
{
UIGraphicsBeginImageContext(clipRect.size);
[clipImagedrawInRect:CGRectMake(0,0,clipRect.size.width,clipRect.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
代码二
转自:http://www.cocoachina.com/bbs/read.php?tid=177476
这段代码主要功能是对于不合规定大小或者比例的图片裁剪中间部分,或者按比例缩小,可能有不足的部分,请各位大牛指正:
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
|
+ (UIImage *)handleImage:(UIImage *)originalImage withSize:(CGSize)size
{
CGSize originalsize = [originalImage size];
NSLog
(@
"改变前图片的宽度为%f,图片的高度为%f"
,originalsize.width,originalsize.height);
//原图长宽均小于标准长宽的,不作处理返回原图
if
(originalsize.width
{
return
originalImage;
}
//原图长宽均大于标准长宽的,按比例缩小至最大适应值
else
if
(originalsize.width>size.width && originalsize.height>size.height)
{
CGFloat rate = 1.0;
CGFloat widthRate = originalsize.width/size.width;
CGFloat heightRate = originalsize.height/size.height;
rate = widthRate>heightRate?heightRate:widthRate;
CGImageRef imageRef =
nil
;
if
(heightRate>widthRate)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2-size.height*rate/2, originalsize.width, size.height*rate));
//获取图片整体部分
}
else
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2-size.width*rate/2, 0, size.width*rate, originalsize.height));
//获取图片整体部分
}
UIGraphicsBeginImageContext(size);
//指定要绘画图片的大小
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 0.0, size.height);
CGContextScaleCTM(con, 1.0, -1.0);
CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);
UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog
(@
"改变后图片的宽度为%f,图片的高度为%f"
,[standardImage size].width,[standardImage size].height);
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
return
standardImage;
}
//原图长宽有一项大于标准长宽的,对大于标准的那一项进行裁剪,另一项保持不变
else
if
(originalsize.height>size.height || originalsize.width>size.width)
{
CGImageRef imageRef =
nil
;
if
(originalsize.height>size.height)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2-size.height/2, originalsize.width, size.height));
//获取图片整体部分
}
else
if
(originalsize.width>size.width)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2-size.width/2, 0, size.width, originalsize.height));
//获取图片整体部分
}
UIGraphicsBeginImageContext(size);
//指定要绘画图片的大小
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 0.0, size.height);
CGContextScaleCTM(con, 1.0, -1.0);
CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);
UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog
(@
"改变后图片的宽度为%f,图片的高度为%f"
,[standardImage size].width,[standardImage size].height);
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
return
standardImage;
}
//原图为标准长宽的,不做处理
else
{
return
originalImage;
}
}
|