问题
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