0

How to upload or add an Image to UIImageView directly from iPhone/Ipad Captured camera Image.

I have uploaded an image to UIImageView from photo library.

Now, I want upload an image directly after taken an image through camera to ImageView.

Please suggest me how to implement this.

using IOS 8.0

KkMIW
  • 1,092
  • 1
  • 17
  • 27

2 Answers2

1

This can be accomplished very easily with the Xamarin.Mobile component, which is free and works with all platforms.

http://components.xamarin.com/view/xamarin.mobile

From the example they give:

using Xamarin.Media;
// ...

var picker = new MediaPicker ();
if (!picker.IsCameraAvailable)
    Console.WriteLine ("No camera!");
else {
    try {
        MediaFile file = await picker.TakePhotoAsync (new StoreCameraMediaOptions {
            Name = "test.jpg",
            Directory = "MediaPickerSample"
        });

        Console.WriteLine (file.Path);
    } catch (OperationCanceledException) {
        Console.WriteLine ("Canceled");
    }
}

After you take the picture, it is saved to the directory you specified, with the name you specified. To easily retrieve this picture and display it with your ImageView using the above example you can do the following:

//file is declared above as type MediaFile
UIImage image = new UIImage(file.Path);

//Fill in with whatever your ImageView is
yourImageView.Image = image;

Edit:

Just a note that the above needs to be asynchronous. So if you want to launch the camera from a button call, for instance, you just slightly modify the .TouchUpInside event:

exampleButton.TouchUpInside += async (object sender, EventArgs e) => {
  //Code from above goes in here, make sure you have async after the +=
};

Otherwise you could wrap the code from above in a function and add async to that:

public async void CaptureImage()
{
    //Code from above goes here
}
Coridan
  • 158
  • 5
  • Thanks for the Component..... I added the Component and using Xamarin.Media into ViewController. Error CS4033 I got an error: The `await' operator can only be used when its containing method is marked with the `async' modifier. – KkMIW Sep 19 '14 at 08:00
  • I've updated my answer to include information about using async. Please let me know if you have any further questions about it. – Coridan Sep 19 '14 at 12:21
  • I have issue with it, when run application on mobile device, Its open the Camara and automatically dismissed. – KkMIW Sep 20 '14 at 07:56
  • async public void TakeCameraPicture(){ var picker = new MediaPicker (); if (!picker.IsCameraAvailable) Console.WriteLine ("N.F"); else { try { MediaFile file = await picker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); UIImage image = new UIImage(file.Path); ImageView.Image = image; Console.WriteLine (file.Path); } catch (OperationCanceledException) { Console.WriteLine ("Cancel");} } } – KkMIW Sep 21 '14 at 16:47
  • 1
    camera dismiss with ::::::: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates. 2014-09-21 22:15:13.207 IOS[516:67413] *** Camera: ignoring _previewStarted because waiting for session to be rebuilt – KkMIW Sep 21 '14 at 16:51
0

You will need to use AVFoundation to do this. Check out the AVCam sample project in Xcode:

https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html

joelg
  • 1,094
  • 9
  • 19