App using UIImagePickerController freezes when selecting “Use Photo” after taking a photo

自古美人都是妖i 提交于 2019-12-10 13:00:45

问题


I'm working on a simple photo and video capture app right now. The app successfully allows the user to take a photo or video on a button press. However, as soon as you finish taking your photo or video, it gives 2 options: "Retake" and "Use Photo" or "Use Video", depending on which one you're using.

If the user taps "Retake" then it just let's them retake a new photo or video, but when the user taps "Use photo" or "Use video" the screen freezes.

If I exit the frozen app and go look in my camera roll, the photo I just took using the app has successfully been saved. So when you tap the "Use Photo" button it does successfully save to the camera roll despite the fact that the app screen freezes.

I was told by someone that "it freezes because it takes some time to save the photo and it is running on main thread. You can use the ALAssetLibrary to fix the freezing issue."

However, I am not familiar with ALAssetLibrary or the theory of why it would be helpful in this situation. I just skimmed through some of it's documentation and I am still lost.

Any help would be greatly appreciated.

Here is the code that I have so far:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property UIImagePickerController * pickerController;

-(IBAction)showCameraUI;

@end

ViewController.m:

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>


@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

//The above are the 2 delegates required for creating a media capture/camera app.

- (void)imagePickerController:pickerController didFinishPickingMediaWithInfo:(NSDictionary *)info;


@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

NSLog(@"%ld", (long)UIImagePickerControllerSourceTypeCamera);

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)showCameraUI{

UIImagePickerController * pickerController = [[UIImagePickerController alloc]init];

pickerController.mediaTypes = [UIImagePickerController  availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

pickerController.delegate = self;

[self presentViewController:pickerController animated:YES completion:nil];

}

- (void)imagePickerController:pickerController didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// saves the photo you just took

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
}

}

- (void)imagePickerControllerDidCancel:pickerController
{
[self dismissViewControllerAnimated:YES completion:nil];
}

@end

来源:https://stackoverflow.com/questions/20790004/app-using-uiimagepickercontroller-freezes-when-selecting-use-photo-after-takin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!