简体   繁体   中英

How to set allowsEditing for UIImagePickerController only for videos selected from photo library in iOS?

I'm using UIImagePickerController in my applicaiton. I'm showing the following alert in button action.

在此处输入图片说明

My code looks like the following in alertview delegate method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([alertView.title isEqualToString:@"Choose"]) {
        if (buttonIndex == 1) { //Camera
            [self presentImagePickerControllerWithCamera:YES];
        }
        else if(buttonIndex == 2) { //Library
            [self presentImagePickerControllerWithCamera:NO];
        }
    }
}

and the content of the method 'presentImagePickerControllerWithCamera' is:

- (void)presentImagePickerControllerWithCamera:(BOOL)isCamera {

    CFStringRef mTypes[2] = { kUTTypeImage, kUTTypeMovie };
    CFArrayRef mTypesArray = CFArrayCreate(CFAllocatorGetDefault(), (const void**)mTypes, 2, &kCFTypeArrayCallBacks);
    imagePickerController.mediaTypes = (__bridge NSArray*)mTypesArray;
    imagePickerController.videoMaximumDuration = 60.0f;

    CFRelease(mTypesArray);
    if (isCamera) { // Showing the camera (Both Camera and video)
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;
        imagePickerController.allowsEditing = NO;
    }
    else {  // Showing library (Both Pictures and videos)
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }

    [self presentViewController:imagePickerController animated:YES completion:nil];
}

Here i need to limit the video to 1 min when user is selecting video from library, so i wrote

imagePickerController.videoMaximumDuration = 60.0f;

and

    imagePickerController.allowsEditing = YES;

it's working fine. But When the user selects a picture from library i don't want editing(the square box on picture). Is there any way to achieve it?

Any suggestions would be appreciated. Thanks in advance.

This isn't possible with your current code.

You've assigned both the media types together. That will show you both image& video in to photo gallery. Once UIImagePickerController will show you'll not have access to it, I mean you can't know what user will select (an image or a video) before delegate get call.

So to achieve this with your case, you've to set each media type at once. And based on that media type, have to set allowsEditing property.

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