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

时间:2013-12-26 19:16:52

标签: ios objective-c uiimagepickercontroller alassetslibrary

我正在制作一个简单的照片和视频捕捉应用程序。该应用程序成功允许用户在按下按钮时拍摄照片或视频。但是,只要您完成拍摄照片或视频,它就会提供两个选项:" Retake"和"使用Photo"或"使用视频",具体取决于您正在使用的视频。

如果用户点击"重拍"然后它只是让他们重新拍摄一张新的照片或视频,但是当用户点击"使用照片"或"使用视频"屏幕冻结。

如果我退出冻结的应用程序并查看我的相机胶卷,我刚刚使用该应用程序拍摄的照片已成功保存。所以当你点击"使用照片"按钮它成功保存到相机胶卷,尽管应用程序屏幕冻结。

有人告诉我,它会冻结,因为它需要一些时间来保存照片并且它正在主线程上运行。您可以使用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

0 个答案:

没有答案