Saving large images (such as those taken with the camera on the iPhone 4 / 4S) is time consuming. If you profile the process, you will find that UIImagePNGRepresentation()
takes some time to generate your PNG image, but the main bottleneck in my experience was writing to a disk with a 1+ MB image.
Not only can you do to speed up this process, in addition to using JPEG compression, which I found in my tests a little faster, or using a faster third-party image compression procedure. Therefore, if you do not want to block your user interface while this happens, send this save process to a background thread or queue. You can do something like the following:
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage* image = [info objectForKey: UIImagePickerControllerOriginalImage]; NSString* imageName = @"Receipt1Image1"; [self saveImage:image :imageName]; }); }
However, be aware that each of these UIImages uses quite a bit of memory for storage, so you can use the send semaphore to prevent several such image-saving operations from being performed simultaneously.
Just like a stylistic note, an Objective-C method definition of type
- (void)saveImage:(UIImage*)image:(NSString*)imageName
while allowed, very discouraged. Give each parameter a name, for example, the following:
- (void)saveImage:(UIImage*)image fileName:(NSString*)imageName
This will make your code more understandable.
source share