iPhone:从 UIImagePickerController 调整图像大小时出现问题
我想要实现的目标:
目前,我设置了一个 UIImagePickerController 来拍照,并将其作为小尺寸照片(大约 300 x 500 像素)返回。
默认情况下,从图像选择器获取的图像是全分辨率的 - 即相当大。因此,在对此图像执行任何操作之前,我会调整其大小:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
-
效果不太好:
此步骤非常耗费资源,事实上,它会在操作过程中使应用程序冻结几秒钟。我还没有在 iPhone 4 上尝试过,但我可以想象,如果使用高清图像,情况只会更糟。
除了占用大量资源之外,获取这张照片似乎还会在短时间内消耗大量内存,通常会导致应用程序崩溃。我假设这个内存来自最初捕获的大尺寸图像。
-
那么:
从 UIImagePicker 获取小图像的最佳/标准方法是什么?我不希望我的应用程序由于 CPU 或 RAM 使用率过高而冻结或崩溃。肯定有更好、更稳定、更高效的方法吗?
非常感谢任何帮助:)
What I'm trying to achieve:
At the moment, I have a UIImagePickerController set up to take a picture, and return it as a small-sized photo (about 300 x 500ish pixels).
By default, the image obtained from the image picker is full resolution - i.e. quite large. So before doing anything with this image, I resize it:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
-
What doesn't work so well:
This step is quite resource intensive, in fact it freezes the app for several seconds during the operation. I haven't tried it on an iPhone 4, but I can imagine it will only be worse with HD images.
Aside from being resource intensive, getting this photo also seems to use up a very large amount of memory for a small period of time, often resulting in the app crashing. I am assuming that this memory is from the large sized images that are being captured at first.
-
So:
What it the best/standard method of getting small images from UIImagePicker? I don't want my app to freeze or crash due to high CPU or RAM usage. Surely there must be a better, more stable and efficient way?
Any help is much appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现 Trevor 的 UIImage 类别对于以正确的方式调整图像大小很有用,而不是重新发明轮子。这就是我使用的并且效果很好。
以正确的方式调整 UIImage 的大小
至于性能问题,您可以在后台调用调整大小方法,或者将其放在一个操作中以释放主线程的这项工作。
You may find the Trevor's UIImage categories useful to resize images the right way and not reinventing the wheel. That's what I use and it works great.
Resize a UIImage the right way
As for the performance issue, you may call your resize method in the background, or put it in an operation to free your main thread of this work.