iOS開發>學無止境 - 截取UIImage指定大小地區

來源:互聯網
上載者:User

標籤:

最近遇到這樣的需求:從伺服器擷取到一張照片,只需要顯示他的左半部分,或者中間部分等等。也就是截取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即可實現。以上代碼依然在iOSStrongDemo裡。

iOS開發>學無止境 - 截取UIImage指定大小地區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.