How to speed up saving a UIImagePickerController image from a camera to a file system via UIImagePNGRepresentation ()?

I am making applications that allow users to take a picture and show them in both thumbnail and photo view mode. I have an NSManagedObject class called photo and photo that uses UIImage and converts it to PNG using UIImagePNGRepresentation () and saves it in the file system. After this operation, resize the image to a thumbnail and save it.

The problem here is in UIImagePNGRepresentation (), and converting the image size seems very slow, and I don't know if this is right.

Tell me if anyone knows how best to accomplish what I want to do.

Thanks in advance.

+3
source share
4 answers

Depending on the resolution of the image, UIImagePNGRepresentation can be quite slow, like any write to the file system.

You should always perform these types of operations in an asynchronous queue. Even if the performance seems good enough for your application when testing, you should still make it an asynchronous queue - you never know what other processes can occur on the device, which can slow down saving after your application is in the hands of users.

Newer versions of iOS make it possible to save asynchronously, very simply, using Grand Central Dispatch (GCD). Steps:

  • Create an NSBlockOperation that Saves the Image
  • In the block for completing the operation of the block, read the image from the disk and display it. The only caveat is that you should use the main queue to display the image: all user interface operations must be performed in the main thread.
  • Add a block operation to the operation queue and see how it happens!

What is it. And here is 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]; 

Once you like this code template, you can use it. This is incredibly useful when creating large data sets, lengthy load operations, etc. In fact, any operation that makes your user interface concise is at least a good candidate for this code. Just remember, you can’t do anything with the interface until you’re in the main line and the rest is cake.

+2
source

Yes, iPhone 4 takes time, where the image size is about 6 MB. The solution is to execute UIImagePNGRepresentation() in the background thread using performSelectorInBackground:withObject: so that your user interface thread does not hang.

0
source

Most likely, it will be much faster to do the resizing before converting to PNG.

-1
source

Try the UIImageJPEGRview with medium compression quality. If the bottleneck is IO, this may turn out to be faster since the file size will usually be smaller than png.

Use the "Tools" to check if the UIImagePNGR view is slow, or writes data to a slow file system.

-2
source

Source: https://habr.com/ru/post/1416383/


All Articles