ios – 使用UIImagePickerControllerCropRect裁剪UIImagePickerControllerOriginalImage返回不正确的图像

前端之家收集整理的这篇文章主要介绍了ios – 使用UIImagePickerControllerCropRect裁剪UIImagePickerControllerOriginalImage返回不正确的图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试编辑捕获的图像并将其保存到图库.我做了
  1. UIImagePickerController *picker=[[UIImagePickerController alloc] init];
  2. picker.allowsEditting=YES;

我想将图像保存在可编辑的方形部分并将其保存到图库.我知道我可以使用[info objectForKey:@“UIImagePickerControllerEditedImage”]来保存编辑过的图像.但这总是让我看到尺寸为320×320(iPad Mini)的图像,图像质量很差.所以我打算使用以下代码裁剪原始图像[info objectForKey:@“UIImagePickerControllerOriginalImage”]:

  1. CGRect rect = [[info objectForKey:@"UIImagePickerControllerCropRect"]CGRectValue];
  2. UIImage *originalImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
  3. CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage],rect);
  4. UIImage *result = [UIImage imageWithCGImage:imageRef
  5. scale:originalImage.scale
  6. orientation:originalImage.imageOrientation];
  7. CGImageRelease(imageRef);

然后我保存了结果图像和编辑图像([info objectForKey:@“UIImagePickerControllerEditedImage”]).当比较两个图像时,它们匹配.我附加了编辑和裁剪的图像.我的最终目标是将原始图像裁剪为可编辑方形部分中的图像,并将其保存到具有良好图像质量的图库中.谁能告诉我这里到底出了什么问题并帮我解决这个问题?

提前致谢.

解决方法

我发现了种植错误的原因. UIImagePickerControllerOriginalImage返回的图像旋转到-90度.因此,在旋转的图像上裁剪会返回错误的裁剪图像.所以我将图像旋转到90度然后裁剪它.最后我得到了预期的裁剪图像,质量很好.以下代码解决了我的问题.
  1. UIImage *originalImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  2. CGRect rect=[[info objectForKey:@"UIImagePickerControllerCropRect"]CGRectValue];
  3. UIImage *rotatedOriginalImage=[originalImage imageRotatedByDegrees:90.0];
  4. CGImageRef imageRef = CGImageCreateWithImageInRect([rotatedOriginalImage CGImage],rect) ;
  5. UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];

用于旋转图像的代码

  1. - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees{
  2. // calculate the size of the rotated view's containing Box for our drawing space
  3. UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,self.size.height,self.size.width)];
  4. CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
  5. rotatedViewBox.transform = t;
  6. CGSize rotatedSize = rotatedViewBox.frame.size;
  7.  
  8.  
  9. // Create the bitmap context
  10. UIGraphicsBeginImageContext(rotatedSize);
  11. CGContextRef bitmap = UIGraphicsGetCurrentContext();
  12.  
  13. // Move the origin to the middle of the image so we will rotate and scale around the center.
  14. CGContextTranslateCTM(bitmap,rotatedSize.width/2,rotatedSize.height/2);
  15.  
  16. // // Rotate the image context
  17. CGContextRotateCTM(bitmap,DegreesToRadians(degrees));
  18.  
  19. // Now,draw the rotated/scaled image into the context
  20. CGContextScaleCTM(bitmap,1.0,-1.0);
  21. CGContextDrawImage(bitmap,CGRectMake(-self.size.height / 2,-self.size.width / 2,self.size.width),[self CGImage]);
  22.  
  23. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  24. UIGraphicsEndImageContext();
  25. return newImage;
  26. }

猜你在找的iOS相关文章