将UIImage转换为CIImage以裁剪为CGRect。 AVFoundation

所以我有一个想要裁剪的UIImage。 我查看并找到了CIImage的imageByCroppingToRect方法。 因此,我将数据转换为CIImage而不是UIImage,使用指定的方法裁剪它,然后将生成的CIImage转换为UIImage,然后在UIImageView中显示它。

我的代码是

  NSData *data = [[NSData alloc]initWithData:[def objectForKey:@"imageData"]]; //UIImage *normalImage = [[UIImage alloc]initWithData:data]; CIImage *originalImage = [CIImage imageWithData:data]; [originalImage imageByCroppingToRect:CGRectMake(10, 72, 300, 300)]; self.imageView.image = [UIImage imageWithCIImage:originalImage]; 

问题是图像旋转了90度,我不确定它是否被裁剪。 使用设备的相机捕获此图像。 我使用AVFoundation访问相机。 我的会话预设是AVCaptureSessionPresetPhoto 。 我想这就是我进行缩放的原因。

 CGRect rect = CGRectMake(10, 72, 300, 300); CGImageRef imref = CGImageCreateWithImageInRect([yourOriginalImage CGImage], rect); UIImage *newSubImage = [UIImage imageWithCGImage:imref]; 

尝试这个。 可能会帮助你。

编辑:

首先修复你的图像方向:参考: https : //github.com/j3r3miah/mapmatic-ios/blob/master/Mapmatic/UIImage+FixOrientation.m

然后使用上面的代码将Image裁剪为指定的Rect。

不是你问题的答案,而是你问题的答案

https://github.com/mbcharbonneau/UIImage-Categories

特别是这个文件: https : //github.com/mbcharbonneau/UIImage-Categories/blob/master/UIImage%2BResize.m

 - (UIImage *)croppedImage:(CGRect)bounds { CGFloat scale = MAX(self.scale, 1.0f); CGRect scaledBounds = CGRectMake(bounds.origin.x * scale, bounds.origin.y * scale, bounds.size.width * scale, bounds.size.height * scale); CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], scaledBounds); UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:UIImageOrientationUp]; CGImageRelease(imageRef); return croppedImage; } 

你会发现你需要裁剪你的图像