iOS Uiimagepickercontroller: Any Way of Getting the Date of the Chosen Picture

How to get picture directly once we capture it? (Without usephoto/cancel step in the middle) (iOS 7)

Step 1 : Remove default camera controls by this method.

[pickerController setShowsCameraControls:NO];

Step 2 : Create an Overlay view and put one button on the overlay view to capture image.Set the view as picker controller's overlay view.

[pickerController setCameraOverlayView:camOverlay];

In the button action write this method:

[pickerController takePicture];

This automatically calls the delegate method and we will get the image directly with out use photo or cancel Step.

NOTE: Source type of picker controller should be camera to implement the above stuff.

pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

set delegate to self.

How to get image from UIImagePickerController and Pass to next VC

  1. You need to assign the delegate to your image picker so that the appropriate methods to be called. In your loadImageButtonTapped function, don't forget to assign the image picker delegate.

Then, you should implement this method:

    func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){ 
// check if an image was selected,
// since a images are not the only media type that can be selected
if let img = info[.originalImage] {
methodToPassImageToViewController(img)
}

If you need to find out the size of the image, you cann access it's size property.


  1. To pass the image to another view controller, you could use one of these two options:

    • you could use delegation
    • you could use this function for when you navigate from your view controller to another one using storyboard segues:

      func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) 
      {
      var destinationController = segue.destinationViewController
      // now you can pass the image to the destination view controller
      }

P.S: didFinishPickingImage is deprecated since iOS 3, you should not use that method.

Image Picker in SwiftUI - how do I know when the user has chosen an image in the hosting UIkit based ViewController

So it was as simple as putting the call to the Api call in the picker call back in UIViewControllerRepresentable

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let uiImage = info[.originalImage] as? UIImage {
parent.image = uiImage
apiHelper.uploadProfileImage(imageToUpload: uiImage) { (success, _) in
if success {
print("IMAGE UPLOADED")
} else {
print("IMAGE UPLOAD FAILED")
}
}

}
parent.presentationMode.wrappedValue.dismiss()
}

How can I keep track of media created/chosen by UIImagePickerController?

I've finally managed to find a solution:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if(CFStringCompare((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
{
//Dismiss the media picker view
[picker dismissModalViewControllerAnimated:YES];

//Get the URL of the chosen content, then get the data from that URL
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];

//Gets the path for the URL, to allow it to be saved to the camera roll
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath))
{
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];

//The key UIImagePickerControllerReferenceURL allows you to get an ALAsset, which then allows you to get metadata (such as the date the media was created)
[lib assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
NSLog(@"created: %@", [asset valueForProperty:ALAssetPropertyDate]);
} failureBlock:^(NSError *error) {
NSLog(@"error: %@", error);
}];
}
}

As per usual, the solution was found by reading the documentation a little more thoroughly. Hopefully this'll help someone else out at some point.



Related Topics



Leave a reply



Submit