【发布时间】:2014-09-18 14:34:03
【问题描述】:
如何直接从 iPhone/Ipad 捕获的相机图像上传或添加图像到 UIImageView。
我已将图片从照片库上传到 UIImageView。
现在,我想在通过相机拍摄图像后直接将图像上传到 ImageView。
请建议我如何实现它。
使用 IOS 8.0
【问题讨论】:
标签: c# ios uiimageview xamarin image-capture
如何直接从 iPhone/Ipad 捕获的相机图像上传或添加图像到 UIImageView。
我已将图片从照片库上传到 UIImageView。
现在,我想在通过相机拍摄图像后直接将图像上传到 ImageView。
请建议我如何实现它。
使用 IOS 8.0
【问题讨论】:
标签: c# ios uiimageview xamarin image-capture
这可以通过 Xamarin.Mobile 组件轻松完成,该组件免费且适用于所有平台。
http://components.xamarin.com/view/xamarin.mobile
从他们给出的例子中:
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");
}
}
拍照后,它会以您指定的名称保存到您指定的目录中。要使用上面的示例轻松检索此图片并使用 ImageView 显示它,您可以执行以下操作:
//file is declared above as type MediaFile
UIImage image = new UIImage(file.Path);
//Fill in with whatever your ImageView is
yourImageView.Image = image;
编辑:
请注意,以上内容需要是异步的。因此,例如,如果您想通过按钮调用启动相机,您只需稍微修改.TouchUpInside 事件:
exampleButton.TouchUpInside += async (object sender, EventArgs e) => {
//Code from above goes in here, make sure you have async after the +=
};
否则,您可以将上面的代码包装在一个函数中并添加异步:
public async void CaptureImage()
{
//Code from above goes here
}
【讨论】:
await' operator can only be used when its containing method is marked with the async' 修饰符。
您需要使用 AVFoundation 来执行此操作。查看 Xcode 中的 AVCam 示例项目:
https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html
【讨论】: