これは、使用している画像のサイズが通常 2 MB を超える非常に大きいためです。そのため、使用する前に画像のサイズを変更します。コードスニペットは次のとおりです。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
if (!originalImage)
return;
// Optionally set a placeholder image here while resizing happens in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Set desired maximum height and calculate width
CGFloat height = 640.0f; // or whatever you need
CGFloat width = (height / self.view.frame.size.height) * self.view.frame.size.width;
// Resize the image
UIImage * image = [originalImage resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationDefault];
//save/use the image here
UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);//for saving to camera roll
});
}
}