iPhone:从 UIImagePickerController 调整图像大小时出现问题

发布于 2024-10-31 12:39:14 字数 885 浏览 2 评论 0 原文

我想要实现的目标:

目前,我设置了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2024-11-07 12:39:14

您可能会发现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文