【问题标题】:Capture Image From Camera set to ImageView从设置为 ImageView 的相机捕获图像
【发布时间】:2014-09-18 14:34:03
【问题描述】:

如何直接从 iPhone/Ipad 捕获的相机图像上传或添加图像到 UIImageView。

我已将图片从照片库上传到 UIImageView。

现在,我想在通过相机拍摄图像后直接将图像上传到 ImageView。

请建议我如何实现它。

使用 IOS 8.0

【问题讨论】:

    标签: c# ios uiimageview xamarin image-capture


    【解决方案1】:

    这可以通过 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
    }
    

    【讨论】:

    • 感谢组件.....我添加了组件并在 ViewController 中使用 Xamarin.Media。错误 CS4033 我收到错误:await' operator can only be used when its containing method is marked with the async' 修饰符。
    • 我已更新我的答案以包含有关使用异步的信息。如果您对此还有任何疑问,请告诉我。
    • 我有问题,在移动设备上运行应用程序时,它会打开 Camara 并自动关闭。
    • async public void TakeCameraPicture(){ var picker = new MediaPicker (); if (!picker.IsCameraAvailable) Console.WriteLine ("N.F");否则 { 尝试 { MediaFile 文件 = await picker.TakePhotoAsync (new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); UIImage 图像 = 新 UIImage(file.Path); ImageView.Image = 图像; Console.WriteLine (file.Path); } catch (OperationCanceledException) { Console.WriteLine ("Cancel");} } }
    • camera dismiss with ::::::: 对未渲染的视图进行快照会导致快照为空。确保在快照或屏幕更新后的快照之前至少渲染一次视图。 2014-09-21 22:15:13.207 IOS[516:67413] *** 相机:忽略 _previewStarted 因为等待会话重建
    【解决方案2】:

    您需要使用 AVFoundation 来执行此操作。查看 Xcode 中的 AVCam 示例项目:

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

    【讨论】:

    • 我可以在 xamarin Cross Platform 中拥有同样的东西吗?
    • 我会这么认为,虽然我不使用 Xamarin。
    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多