iOS應(yīng)用開(kāi)發(fā)中對(duì)UIImage進(jìn)行截取和縮放的方法詳解
截取UIImage指定大小區(qū)域
最近遇到這樣的需求:從服務(wù)器獲取到一張照片,只需要顯示他的左半部分,或者中間部分等等。也就是截取UIImage指定大小區(qū)域。
UIImage擴(kuò)展:
我的解決方案是對(duì)UIImage進(jìn)行擴(kuò)展。通過(guò)CGImageRef和CGImage完成截取,調(diào)用的方法是:CGImageCreateWithImageInRect。擴(kuò)展類叫UIImage+Crop,具體代碼如下:
UIImage+Crop.h
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, XYCropImageStyle){
XYCropImageStyleRight =0, // 右半部分
XYCropImageStyleCenter =1, // 中間部分
XYCropImageStyleLeft =2, // 左半部分
XYCropImageStyleRightOneOfThird =3, // 右側(cè)三分之一部分
XYCropImageStyleCenterOneOfThird =4, // 中間三分之一部分
XYCropImageStyleLeftOneOfThird =5, // 左側(cè)三分之一部分
XYCropImageStyleRightQuarter =6, // 右側(cè)四分之一部分
XYCropImageStyleCenterRightQuarter =7, // 中間右側(cè)四分之一部分
XYCropImageStyleCenterLeftQuarter =8, // 中間左側(cè)四分之一部分
XYCropImageStyleLeftQuarter =9, // 左側(cè)四分之一部分
};
@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;
}
實(shí)際運(yùn)用:
簡(jiǎn)單測(cè)試一下,看看有沒(méi)有實(shí)現(xiàn)我們想要的效果。首先,先加載一個(gè)完整的UIImageView。這個(gè)應(yīng)該不難。代碼如下:
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];
運(yùn)行一下:

要對(duì)UIImage進(jìn)行裁剪,首先導(dǎo)入頭文件:
#import "UIImage+Crop.h"
在上面UIImage *image = [UIImage imageNamed:@"ganggang"];這段代碼之后加上下面這句:
image = [image imageByCroppingWithStyle:XYCropImageStyleLeft];
XYCropImageStyleLeft是截取照片的左半部分。效果如下:

截取成功,還可以截取其他區(qū)域的,只需要傳入不同的XYCropImageStyle即可實(shí)現(xiàn)。
UIImage等比縮放
前面講了截取UIImage指定大小區(qū)域,很方便的截取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)通過(guò)縮放系數(shù):
- (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是縮放系數(shù) 。
(2)通過(guò)計(jì)算得到縮放系數(shù)
- (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;
}
- iOS應(yīng)用開(kāi)發(fā)中使用UIScrollView控件來(lái)實(shí)現(xiàn)圖片縮放
- iOS UITableView展開(kāi)縮放動(dòng)畫(huà)實(shí)例代碼
- iOS開(kāi)發(fā)中Quartz2D控制圓形縮放和實(shí)現(xiàn)刷幀效果
- iOS實(shí)現(xiàn)點(diǎn)擊微信頭像(放大、縮放、保存)效果
- iOS tableView實(shí)現(xiàn)頭部拉伸并改變導(dǎo)航條漸變色
- iOS開(kāi)發(fā)之tableView實(shí)現(xiàn)左滑刪除功能
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- 解決iOS11刷新tableview會(huì)出現(xiàn)漂移的現(xiàn)象
- IOS實(shí)現(xiàn)左右兩個(gè)TableView聯(lián)動(dòng)效果
- iOS TableView頭視圖根據(jù)偏移量下拉縮放效果
相關(guān)文章
iOS多線程應(yīng)用開(kāi)發(fā)中自定義NSOperation類的實(shí)例解析
這篇文章主要介紹了iOS多線程應(yīng)用開(kāi)發(fā)中自定義NSOperation類的實(shí)例解析,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
iOS UICollectionView實(shí)現(xiàn)標(biāo)簽選擇器
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實(shí)現(xiàn)標(biāo)簽選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了詳解iOS AFNetworking取消正在進(jìn)行的網(wǎng)絡(luò)請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
實(shí)例解析iOS應(yīng)用多線程開(kāi)發(fā)中NSthread類的用法
這篇文章主要介紹了iOS應(yīng)用多線程開(kāi)發(fā)中NSthread類的用法,代碼基于傳統(tǒng)的Objective-C,NSthread類需要的朋友可以參考下2016-02-02
iOS App使用設(shè)計(jì)模式中的模板方法模式開(kāi)發(fā)的示例
這篇文章主要介紹了iOS應(yīng)用使用設(shè)計(jì)模式中的模板方法模式開(kāi)發(fā)的示例,例子代碼為Objective-C語(yǔ)言,文中還與Java的相關(guān)實(shí)現(xiàn)進(jìn)行類比,需要的朋友可以參考下2016-03-03
詳談iOS 位置權(quán)限彈出框閃現(xiàn)的問(wèn)題
下面小編就為大家?guī)?lái)一篇詳談iOS 位置權(quán)限彈出框閃現(xiàn)的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
iOS 高德地圖仿微信發(fā)送實(shí)時(shí)位置
這篇文章主要介紹了iOS 高德地圖仿微信發(fā)送實(shí)時(shí)位置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

