Select a video from the iPhone photo library

I am trying to select a video from the iPhone photo library using UIImagePickerController so that I can upload it to the server. I can select images using the UIImagePickerController without any errors / crashes, but, choosing (not pressing the select button, but just selecting a video from the library), my application crashes. It simply prints the following lines on the console:

setting the path to the movie: (null)

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

Event: the didFinishPickingMediaWithInfo delegate method is not called before the application crashes. I just can't figure out what actually causes the problem.

Below is my code to open 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 .. Thank you!

+4
source share
2 answers

Try

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

Everything looks good.

Have you configured the delegate in the header file?

I can offer only 2 things:

  • Try declaring the imagePicker property as.

2. When you check if this image or video is there, replace

[mediaType isEqualToString:@"public.image"]

from

[mediaType isEqualToString:(NSString *)kUTTypeImage]

and leave a video for the else statement.

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

Change . You can also try addingimagePicker.allowsEditing = NO;

0
source

Source: https://habr.com/ru/post/1532184/


All Articles