简体   繁体   English

从相机捕获图像设置为ImageView

[英]Capture Image From Camera set to ImageView

How to upload or add an Image to UIImageView directly from iPhone/Ipad Captured camera Image. 如何直接从iPhone / Ipad捕获的相机图像上载图像或将图像直接添加到UIImageView。

I have uploaded an image to UIImageView from photo library. 我已将图片从照片库上传到UIImageView。

Now, I want upload an image directly after taken an image through camera to ImageView. 现在,我想在通过相机拍摄图像后将图像直接上传到ImageView。

Please suggest me how to implement this. 请建议我如何执行此操作。

using IOS 8.0 使用IOS 8.0

This can be accomplished very easily with the Xamarin.Mobile component, which is free and works with all platforms. 使用Xamarin.Mobile组件可以很容易地完成此任务,该组件是免费的,并且可以在所有平台上使用。

http://components.xamarin.com/view/xamarin.mobile 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: 要使用上述示例轻松检索这张图片并将其与ImageView一起显示,您可以执行以下操作:

//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: 因此,例如,如果您想通过按钮调用来启动相机,则只需稍微修改.TouchUpInside事件即可:

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
}

You will need to use AVFoundation to do this. 您将需要使用AVFoundation来执行此操作。 Check out the AVCam sample project in Xcode: 在Xcode中查看AVCam示例项目:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM