gpt4 book ai didi

ios - UIImagePickerController 错误

转载 作者:IT王子 更新时间:2023-10-29 07:56:12 24 4
gpt4 key购买 nike

我想我通过运行一个将 Base SDK 设置为 iOS 6.1 的应用程序在最新的 iOS 7 中发现了一个错误(可能还有更低的版本,还没有测试过)

我的照片库中有这张图片:http://i.imgur.com/7KUIGLt.jpg

我通过以下方式呈现 UIImagePickerController:

UIImagePickerController *vc = [[UIImagePickerController alloc] init];
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
vc.delegate = self;
vc.allowsEditing = YES;
[self presentViewController:vc animated:YES completion:nil];

我将选择的图像保存到我的桌面(我在模拟器上运行它,但它也适用于设备)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage* outputImage = [info objectForKey:UIImagePickerControllerEditedImage];
if (outputImage == nil) {
outputImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
NSData *d = UIImagePNGRepresentation(outputImage);
[d writeToFile:@"/Users/Admin/Desktop/test.png" atomically:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}

这是生成的图像: enter image description here

注意右边的大黑条。这是什么原因造成的?

要重现这一点,您需要:

  • iOS 7
  • 将 Base SDK 设置为 6.1 的应用(可能还有更低的 SDK,我还没有尝试过)
  • iPhone 5/5c/5s
  • 仅发生在使用 iPhone 5/5c/5s 相机拍摄的照片上(您可以使用我上面链接的原始图像进行测试)

注意:请注意,黑条是实际图像的一部分。您看到的图像不是 UIImageView 的屏幕截图,而是保存到磁盘并上传到此处的实际图像...

最佳答案

您的问题是“这是什么原因造成的?”因此,我将专注于此,而不是提供解决方法。这绝对是 iOS 7 处理较低基础 SDK 中编辑图像的错误。我们还可以排除 XCode 5 和 Base SDK 6.1 导致此问题的可能性,因为我在 iOS 7 模拟器上运行的 XCode 4.6.3 和 6.1 SDK 遇到了同样的问题。

问题的根源在于SDK计算出的CropRect值有误。如果您从 imagePickerController:didFinishPickingMediaWithInfo 打印出 info NSDictionary,您会看到:

iOS 7 运行任何低于 7 的 SDK 我们会得到:

UIImagePickerControllerCropRect = "NSRect: {{154, 495}, {1705, 1705}}";

当使用他们的 SDK 运行 iOS 6 或 5 时,我们将得到:

UIImagePickerControllerCropRect = "NSRect: {{0, 149}, {1704, 1705}}";

您可能会说,嗯,这些 y 值也在 SDK 之间发生变化。好吧,是的,如果您将图片一直向下滑动并选择它,您还会在图片底部看到一个黑条。

enter image description here

建议的解决方案:

  1. File a bug report to Apple here ...做过某事!

  2. 不要使用 UIImagePickerControllerEditedImage 而是拍摄原始图片。

  3. 自行计算并裁剪。

  4. 使用第 3 方裁剪库,例如 PEPhotoCropEditorSSPhotoCropperViewController


编辑 - 答案的粉丝添加的非常简单的解决方案!

令人惊讶的是,自己裁剪它可以如此简单和优雅:

{
// There is a bug in iOS. When using ALBUM, you must crop it yourself:

fromAlbum = [info objectForKey:UIImagePickerControllerOriginalImage];
fromAlbum = [fromAlbum fixOrientation];

CGRect crop = [[info valueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
fromAlbum = [self ordinaryCrop:fromAlbum toRect:crop];
}

下面是整个例程 ordinaryCrop:toRect:

-(UIImage *)ordinaryCrop:(UIImage *)imageToCrop toRect:(CGRect)cropRect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], cropRect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}

正如 Jesse 指出的那样,正确旋转图像至关重要。 Anomie 的这段绝对令人难以置信的代码可以完成这项工作:

iOS UIImagePickerController result image orientation after upload

修复 UIImage 方向 .. UIImage+fixOrientation.h

就这么简单,希望对大家有所帮助。再次感谢这里的无价答案。

关于ios - UIImagePickerController 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19397873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com