简体   繁体   中英

Pick a video from iPhone photo library

I am trying to pick a video from iPhone photo library using UIImagePickerController so that i can upload it to the server. I am able to pick the images using UIImagePickerController without any error/Crash, but while choosing(Not on actual clicking on choose button, but just selecting an video from the library) video my application crashes. It just prints following lines on the console:

setting movie path: (null)

setting movie path: /Users/Mahesh/Library/Application Support/iPhone Simulator/6.1/Media/DCIM/100APPLE/IMG_0006.mp4

Event the delegate method didFinishPickingMediaWithInfo does not get called, before it is called my application crashes. I'm just not being able to figure out what actually is causing the problem.

Following is my code for opening the iPhone Library :

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//imagePicker.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];

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


- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
    UIImage *editedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // Compressing the image size
    CGSize newSize = CGSizeMake(400, 400);
    UIGraphicsBeginImageContext(newSize);

    [editedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();

    NSData *data =  UIImageJPEGRepresentation(newImage,0.2);//PNGRepresentation(imgView.image);

}
else if ([mediaType isEqualToString:@"public.movie"]){

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
}

[self dismissViewControllerAnimated:YES completion:nil];

}

Please help me.. Thanks!

尝试

picker.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];

Everything looks good.

Did you set up delegate in header file?

I can only suggest 2 things:

  1. Try declaring imagePicker as property.

2.When you check if it's image or video, replace

[mediaType isEqualToString:@"public.image"]

with

[mediaType isEqualToString:(NSString *)kUTTypeImage]

and leave video for else statement.

You will need to import <MobileCoreServices/UTCoreTypes.h> for this check. Hope this helps.

Edit : You can also try to add imagePicker.allowsEditing = NO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM