iOS应用开发中对UIImage进行截取和缩放的方法详解
程序员文章站
2023-11-28 23:41:04
截取uiimage指定大小区域
最近遇到这样的需求:从服务器获取到一张照片,只需要显示他的左半部分,或者中间部分等等。也就是截取uiimage指定大小区域。
uiima...
截取uiimage指定大小区域
最近遇到这样的需求:从服务器获取到一张照片,只需要显示他的左半部分,或者中间部分等等。也就是截取uiimage指定大小区域。
uiimage扩展:
我的解决方案是对uiimage进行扩展。通过cgimageref和cgimage完成截取,调用的方法是:cgimagecreatewithimageinrect。扩展类叫uiimage+crop,具体代码如下:
uiimage+crop.h
#import <uikit/uikit.h> typedef ns_enum(nsinteger, xycropimagestyle){ xycropimagestyleright =0, // 右半部分 xycropimagestylecenter =1, // 中间部分 xycropimagestyleleft =2, // 左半部分 xycropimagestylerightoneofthird =3, // 右侧三分之一部分 xycropimagestylecenteroneofthird =4, // 中间三分之一部分 xycropimagestyleleftoneofthird =5, // 左侧三分之一部分 xycropimagestylerightquarter =6, // 右侧四分之一部分 xycropimagestylecenterrightquarter =7, // 中间右侧四分之一部分 xycropimagestylecenterleftquarter =8, // 中间左侧四分之一部分 xycropimagestyleleftquarter =9, // 左侧四分之一部分 }; @interface uiimage (crop) - (uiimage *)imagebycroppingwithstyle:(xycropimagestyle)style; @end uiimage+crop.m #import "uiimage+crop.h" @implementation uiimage (crop) - (uiimage *)imagebycroppingwithstyle:(xycropimagestyle)style { cgrect rect; switch (style) { case xycropimagestyleleft: rect = cgrectmake(0, 0, self.size.width/2, self.size.height); break; case xycropimagestylecenter: rect = cgrectmake(self.size.width/4, 0, self.size.width/2, self.size.height); break; case xycropimagestyleright: rect = cgrectmake(self.size.width/2, 0, self.size.width/2, self.size.height); break; case xycropimagestyleleftoneofthird: rect = cgrectmake(0, 0, self.size.width/3, self.size.height); break; case xycropimagestylecenteroneofthird: rect = cgrectmake(self.size.width/3, 0, self.size.width/3, self.size.height); break; case xycropimagestylerightoneofthird: rect = cgrectmake(self.size.width/3*2, 0, self.size.width/3, self.size.height); break; case xycropimagestyleleftquarter: rect = cgrectmake(0, 0, self.size.width/4, self.size.height); break; case xycropimagestylecenterleftquarter: rect = cgrectmake(self.size.width/4, 0, self.size.width/4, self.size.height); break; case xycropimagestylecenterrightquarter: rect = cgrectmake(self.size.width/4*2, 0, self.size.width/4, self.size.height); break; case xycropimagestylerightquarter: rect = cgrectmake(self.size.width/4*3, 0, self.size.width/4, self.size.height); break; default: break; } cgimageref imageref = self.cgimage; cgimageref imagepartref = cgimagecreatewithimageinrect(imageref, rect); uiimage *cropimage = [uiimage imagewithcgimage:imagepartref]; cgimagerelease(imagepartref); return cropimage; }
实际运用:
简单测试一下,看看有没有实现我们想要的效果。首先,先加载一个完整的uiimageview。这个应该不难。代码如下:
uiimageview *imgview = [[uiimageview alloc] init]; imgview.frame = cgrectmake((screen.width - 226) / 2, 100, 226, 106); uiimage *image = [uiimage imagenamed:@"ganggang"]; imgview.image = image; [self.view addsubview:imgview];
运行一下:
要对uiimage进行裁剪,首先导入头文件:
#import "uiimage+crop.h"
在上面uiimage *image = [uiimage imagenamed:@"ganggang"];这段代码之后加上下面这句:
image = [image imagebycroppingwithstyle:xycropimagestyleleft];
xycropimagestyleleft是截取照片的左半部分。效果如下:
截取成功,还可以截取其他区域的,只需要传入不同的xycropimagestyle即可实现。
uiimage等比缩放
前面讲了截取uiimage指定大小区域,很方便的截取uiimage。今天要和大家分享的是uiimage的缩放。
两种缩放:
- 缩放到指定大小,也就是指定的size.
- 等比缩放。
1.缩放到指定大小
- (uiimage*)imagecompresswithsimple:(uiimage*)image scaledtosize:(cgsize)size { uigraphicsbeginimagecontext(size); [image drawinrect:cgrectmake(0,0,size.width,size.height)]; uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return newimage; }
2.等比缩放
(1)通过缩放系数:
- (uiimage*)imagecompresswithsimple:(uiimage*)image scale:(float)scale { cgsize size = image.size; cgfloat width = size.width; cgfloat height = size.height; cgfloat scaledwidth = width * scale; cgfloat scaledheight = height * scale; uigraphicsbeginimagecontext(size); // this will crop [image drawinrect:cgrectmake(0,0,scaledwidth,scaledheight)]; uiimage* newimage= uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return newimage; }
scale是缩放系数 。
(2)通过计算得到缩放系数
- (uiimage*)imagebyscalingandcroppingforsize:(cgsize)targetsize { uiimage *sourceimage = [uiimage imagenamed:@"test.jpg"]; uiimage *newimage = nil; cgsize imagesize = sourceimage.size; cgfloat width = imagesize.width; cgfloat height = imagesize.height; cgfloat targetwidth = targetsize.width; cgfloat targetheight = targetsize.height; cgfloat scalefactor = 0.0; cgfloat scaledwidth = targetwidth; cgfloat scaledheight = targetheight; cgpoint thumbnailpoint = cgpointmake(0.0,0.0); if (cgsizeequaltosize(imagesize, targetsize) == no) { cgfloat widthfactor = targetwidth / width; cgfloat heightfactor = targetheight / height; if (widthfactor > heightfactor) scalefactor = widthfactor; // scale to fit height else scalefactor = heightfactor; // scale to fit width scaledwidth= width * scalefactor; scaledheight = height * scalefactor; // center the image if (widthfactor > heightfactor) { thumbnailpoint.y = (targetheight - scaledheight) * 0.5; } else if (widthfactor < heightfactor) { thumbnailpoint.x = (targetwidth - scaledwidth) * 0.5; } } uigraphicsbeginimagecontext(targetsize); // this will crop cgrect thumbnailrect = cgrectzero; thumbnailrect.origin = thumbnailpoint; thumbnailrect.size.width= scaledwidth; thumbnailrect.size.height = scaledheight; [sourceimage drawinrect:thumbnailrect]; newimage = uigraphicsgetimagefromcurrentimagecontext(); if(newimage == nil) nslog(@"could not scale image"); //pop the context to get back to the default uigraphicsendimagecontext(); return newimage; }