如何在目标c中裁剪图像?
用户可以在编辑屏幕上更改默认显示的裁剪框大小。 我试着用下面的代码:
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect { CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); UIImage *cropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return cropped; }
但它裁剪固定的面积。 如何裁剪用户select的区域?
获取裁剪图像:
UIImage *croppedImg = nil; CGRect cropRect = CGRectMake("AS YOu Need"); //set your rect size. croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
使用以下代码调用croppIngimageByImageName:toRect:返回UIImage
方法( 具有特定大小的图像 )
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect { //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15); CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); UIImage *cropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return cropped; }
CGRect clippedRect = CGRectMake(0 ,0,180 ,180); CGImageRef imageRef = CGImageCreateWithImageInRect(imgVw1.image.CGImage, clippedRect); UIImage *newImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); imgVw1Cliped.image=newImage; NSLog(@"%d",imgVw1Cliped.image.imageOrientation);