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

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

概述我正在尝试编辑捕获的图像并将其保存到图库.我做了 UIImagePickerController *picker=[[UIImagePickerController alloc] init];picker.allowsEditting=YES; 我想将图像保存在可编辑的方形部分并将其保存到图库.我知道我可以使用[info objectForKey:@“UIImagePickerControlle 我正在尝试编辑捕获的图像并将其保存到图库.我做了
UIImagePickerController *picker=[[UIImagePickerController alloc] init];picker.allowsEditting=YES;

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

CGRect rect = [[info objectForKey:@"UIImagePickerControllerCropRect"]CGRectValue];UIImage *originalimage=[info objectForKey:@"UIImagePickerControllerOriginalimage"];CGImageRef imageRef = CGImageCreateWithImageInRect([originalimage CGImage],rect);UIImage *result = [UIImage imageWithCGImage:imageRef                                      scale:originalimage.scale                                orIEntation:originalimage.imageOrIEntation];CGImageRelease(imageRef);

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

提前致谢.

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

用于旋转图像的代码:

- (UIImage *)imageRotatedBydegrees:(CGfloat)degrees{// calculate the size of the rotated vIEw's containing Box for our drawing spaceUIVIEw *rotatedVIEwBox = [[UIVIEw alloc] initWithFrame:CGRectMake(0,self.size.height,self.size.wIDth)];CGAffine@R_403_4154@ t = CGAffine@R_403_4154@MakeRotation(degreesToradians(degrees));rotatedVIEwBox.@R_403_4154@ = t;CGSize rotatedSize = rotatedVIEwBox.frame.size;// Create the bitmap contextUIGraphicsBeginImageContext(rotatedSize);CGContextRef bitmap = UIGraphicsGetCurrentContext();// Move the origin to the mIDdle of the image so we will rotate and scale around the center.CGContextTranslateCTM(bitmap,rotatedSize.wIDth/2,rotatedSize.height/2);//   // Rotate the image contextCGContextRotateCTM(bitmap,degreesToradians(degrees));// Now,draw the rotated/scaled image into the contextCGContextScaleCTM(bitmap,1.0,-1.0);CGContextDrawImage(bitmap,CGRectMake(-self.size.height / 2,-self.size.wIDth / 2,self.size.wIDth),[self CGImage]);UIImage *newImage = UIGraphicsGetimageFromCurrentimageContext();UIGraphicsEndImageContext();return newImage;}
总结

以上是内存溢出为你收集整理的ios – 使用UIImagePickerControllerCropRect裁剪UIImagePickerControllerOriginalImage返回不正确的图像全部内容,希望文章能够帮你解决ios – 使用UIImagePickerControllerCropRect裁剪UIImagePickerControllerOriginalImage返回不正确的图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存