Skip navigation

2 x imagePickerController: (UIImagePickerController *) picker?

2628 Views 2 Replies Latest reply: Jan 28, 2011 6:42 AM by Grant Abt1 RSS
Grant Abt1 Calculating status...
Currently Being Moderated
Jan 21, 2011 7:01 AM
I'm creating a test app that has two buttons, one for launching the camera to record a video and the other to load a previously recorded video. The methods used to launch both the camera and the photo library work. However, the delegate for both is the same (according to the Apple docs anyway):

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

What i need is a way of calling this delegate, but for it to recognise if the user has just chosen the video from the photo library or they've just finished recording it.

I've tried having two methods called this, with different code in each one but it fails to build. If i have just one method, and put both chunks of code in, how do i call the code to handle each one separately?

Here is the delegate code i have now. At present it always saves the selected video to the camera roll, even if you've just selected it from there! I guess I need some sort of 'if' statement.

Any help appreciate.

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

[[picker parentViewController] dismissModalViewControllerAnimated: YES];

NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

//**NEED IF STATEMENT HERE TO DIFFERENTIATE BETWEEN CAPTURED MOVIE AND MOVIE FROM LIBRARY**

// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {

NSString *recordedMoviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (recordedMoviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (recordedMoviePath, nil, nil, nil);
}
}

// Handle a movied picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {

NSString *chosenMoviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

// Do something with the picked movie available at moviePath
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath: chosenMoviePath]];

[player.view setFrame:CGRectMake(10.0, 10.0, 150.0, 100.0)];
[picker.parentViewController.view addSubview:player.view];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];

[player play];
}

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[picker release];
}
MacBook Pro 17, Mac OS X (10.6.6)
  • RayNewbie Calculating status...
    Hi Grant -

    It sounds like the correct design would be to have two instances of UIImagePickerController, so I'll assume that's going to be the case.
    Grant Abt1 wrote:
    The methods used to launch both the camera and the photo library work. However, the delegate for both is the same (according to the Apple docs anyway):

    Yes, you can assign the same object as the delegate for two different controllers, but there's no need to do that. If you don't want to branch delegate code based on the caller, you're free to connect your controllers to different delegate objects.
    What i need is a way of calling this delegate ...

    No, that would defeat the purpose of the delegate. The delegate is there for the controller to call at the correct time, since it's the controller that knows when an operation has started, finished or failed, not you. If you've connected each controller to a valid delegate, each will call [imagePickerController:didFinishPickingMediaWithInfo:|http://developer.apple.co m/library/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegateProtocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#// appleref/doc/uid/TP40007069-CH3-SW8] at the correct time.
    ... but for it to recognise if the user has just chosen the video from the photo library or they've just finished recording it.
    I've tried having two methods called this, with different code in each one but it fails to build. If i have just one method, and put both chunks of code in, how do i call the code to handle each one separately?

    I'd need to build a test bed at this end to provide you with working delegate methods that branch correctly based on the caller. But if I did that, I think we'd wind up with the wrong design choice. I'd recommend two separate instances of UIImagePickerController, each connected to a different delegate object (e.g. a different custom subclass of NSObject which implements the [UIImagePickerControllerDelegate Protocol|http://developer.apple.com/library/ios/#documentation/UIKit/Reference/ UIImagePickerControllerDelegateProtocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html%23 //appleref/doc/uid/TP40007069] methods in a different way.

    When button one is pressed, create and configure the controller which launches the camera then waits for its delegate to process the user action and dismiss the picker view. When button two is pressed, create and configure another controller which loads an existing video then similarly waits for its delegate to clean up. From your description, I don't see any interaction between these two controllers. Is there?

    \- Ray
    iMac, Mac OS X (10.5.8)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.