Intercept UIImage Specify size area
Recently encountered the need to get a picture from the server, just show his left half, or the middle part, etc. That is, intercept uiimage the specified size area.
UIImage extension
My solution is to extend it UIImage
. Through CGImageRef
and CGImage
completion of interception, the method of invocation is: CGImageCreateWithImageInRect
. The extension class UIImage+Crop
is called, the code is as follows:
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.hei Ght); Break Case xycropimagestylerightoneofthird:rect = CGRectMake (self.size.width/3*2, 0, SELF.SIZE.WIDTH/3, self.size.he ight); 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.he ight); 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.heigh T); Break Default:break; } cgimageref imageref = self. Cgimage; Cgimageref imagepartref = Cgimagecreatewithimageinrect (imageref, rect); UIImage *cropimage = [UIImage imagewithcgimage:imagepartref]; Cgimagerelease (IMAGEPARTREF); return cropimage;}
Practical use
Simply test it to see if there is any effect we want to make. First, a complete Uiimageview is loaded first. This should not be difficult. The code is as follows:
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];
Run it:
To crop the uiimage, first import the header file:
#import "UIImage+Crop.h"
In the above UIImage *image = [UIImage imageNamed:@"ganggang"];
code, add the following sentence:
image = [image imageByCroppingWithStyle:XYCropImageStyleLeft];
XYCropImageStyleLeft
is to intercept the left half of the photo. The effect is as follows:
Interception succeeds, but also can intercept other areas, only need to pass in different XYCropImageStyle
can be implemented. The above code is still in Iosstrongdemo.
The source of this article just online: http://www.superqq.com/blog/2015/07/26/jie-qu-uiimagezhi-ding-da-xiao-qu-yu/
Intercept UIImage Specify size area