拍照后select“使用照片”时,使用UIImagePickerController的应用程序会冻结

我正在制作一个简单的照片和video捕捉应用程序。 该应用程序成功地允许用户按下button拍摄照片或video。 但是,只要您完成拍摄照片或video,就会有2个选项:“重拍”和“使用照片”或“使用video”,具体取决于您使用的是哪一种。

如果用户点击“重拍”,那么就让他们重拍一张新的照片或video,但是当用户点击“使用照片”或“使用video”时,屏幕会冻结。

如果我退出冻结的应用程序,并看看我的相机胶卷,我刚刚使用该应用程序的照片已成功保存。 所以,当你点击“使用照片”button,即使应用程序屏幕冻结,它也能成功保存到相机上。

有人告诉我,“冻结是因为保存照片需要一些时间,而且它正在主线程上运行,你可以使用ALAssetLibrary来解决冻结问题。”

但是,我不熟悉ALAssetLibrary或者为什么在这种情况下会有帮助的理论。 我只是通过它的一些文档浏览,而我仍然迷失。

任何帮助将不胜感激。

这是我迄今为止的代码:

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