簡體   English   中英

從相機捕獲圖像設置為ImageView

[英]Capture Image From Camera set to ImageView

如何直接從iPhone / Ipad捕獲的相機圖像上載圖像或將圖像直接添加到UIImageView。

我已將圖片從照片庫上傳到UIImageView。

現在,我想在通過相機拍攝圖像后將圖像直接上傳到ImageView。

請建議我如何執行此操作。

使用IOS 8.0

使用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
}

您將需要使用AVFoundation來執行此操作。 在Xcode中查看AVCam示例項目:

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