如何加快通过UIImagePNGRepresentation()将UIImagePickerController图像从相机保存到文件系统? [英] How to speed up saving a UIImagePickerController image from the camera to the filesystem via UIImagePNGRepresentation()?

查看:113
本文介绍了如何加快通过UIImagePNGRepresentation()将UIImagePickerController图像从相机保存到文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,让用户拍摄照片,并在缩略图和照片查看器中显示它们。
我有NSManagedObject类称为photo和photo有一个方法,需要UIImage并将其转换为PNG使用UIImagePNGRepresentation()并将其保存到文件系统。
此操作后,将图像调整为缩略图大小并保存。

I'm making an applications that let users take a photo and show them both in thumbnail and photo viewer. I have NSManagedObject class called photo and photo has a method that takes UIImage and converts it to PNG using UIImagePNGRepresentation() and saves it to filesystem. After this operation, resize the image to thumbnail size and save it.

这里的问题是UIImagePNGRepresentation(),图像大小的转换似乎很慢我不知道这是否是一个正确的方法。

The problem here is UIImagePNGRepresentation() and conversion of image size seems to be really slow and I don't know if this is a right way to do it.

告诉我,如果有人知道完成我想做的最好的方法。

Tell me if anyone know the best way to accomplish what I want to do.

提前感谢。

推荐答案

UIImagePNGRepresentation 确实可以相当慢,以及任何写入文件系统。

Depending on the image resolution, UIImagePNGRepresentation can indeed be quite slow, as can any writing to the file system.

您应该始终在异步队列中执行这些类型的操作。即使测试时您的应用程序的性能似乎足够好,您仍然应该使用异步队列 - 您永远不知道设备可能会发生什么其他进程,这可能会降低保存速度应用程序在用户手中。

You should always execute these types of operations in an asynchronous queue. Even if the performance seems good enough for your application when testing, you should still do it an asynch queue -- you never know what other processes the device might have going on which might slow the save down once your app is in the hands of users.

新版本的iOS使用大中央调度(GCD),异步保存真的很简单。步骤如下:

Newer versions of iOS make saving asynchronously really, really easy using Grand Central Dispatch (GCD). The steps are:


  1. 创建保存图像的 NSBlockOperation
  2. 在块操作的完成块中,从磁盘&显示它。这里唯一要注意的是,您必须使用主队列来显示图片:所有UI操作必须发生在主线程

  3. 将块操作添加到

就是这样。下面是代码:

That's it. And here's the code:

// Create a block operation with our saves
NSBlockOperation* saveOp = [NSBlockOperation blockOperationWithBlock: ^{

   [UIImagePNGRepresentation(image) writeToFile:file atomically:YES];
   [UIImagePNGRepresentation(thumbImage) writeToFile:thumbfile atomically:YES];

}];

// Use the completion block to update our UI from the main queue
[saveOp setCompletionBlock:^{

   [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 

      UIImage *image = [UIImage imageWithContentsOfFile:thumbfile];
      // TODO: Assign image to imageview

   }];
}];

// Kick off the operation, sit back, and relax. Go answer some stackoverflow
// questions or something.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:saveOp];

一旦你习惯这种代码模式,你会发现自己使用了很多。它在生成大型数据集,加载长操作等方面非常有用。基本上,任何使您的UI至少滞后的操作是此代码的良好候选。只要记住,你不能对UI做任何事情,而你不在主队列,一切都是蛋糕。

Once you are comfortable with this code pattern, you will find yourself using it a lot. It's incredibly useful when generating large datasets, long operations on load, etc. Essentially, any operation that makes your UI laggy in the least is a good candidate for this code. Just remember, you can't do anything to the UI while you aren't in the main queue and everything else is cake.

这篇关于如何加快通过UIImagePNGRepresentation()将UIImagePickerController图像从相机保存到文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆