将UIImage保存到个人文件夹,然后通过UIImage.FromFile加载它

时间:2012-06-07 13:36:25

标签: ios xamarin.ios uiimagepickercontroller fromfile

我已经通过UIImagePickerController完成了一个图片选择器。由于内存问题,我想将所选图像保存到光盘,如果需要,可以从文件路径加载它。但我无法让它发挥作用。

如果我直接绑定原始图像,则显示没有任何问题。

代码中的File.Exists返回true,但如果在调试器中观察,则最后一行中的图像为null。非常感谢您的帮助!

                    NSData data = originalImage.AsPNG();

                    string path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
                    string pathTempImage = Path.Combine(path, "tempImage.png");

                    byte[] tempImage = new byte[data.Length];

                    File.WriteAllBytes(pathTempImage, tempImage);

                    if(File.Exists(pathTempImage))
                    {
                        int i = 0;
                    }

                    UIImage image = UIImage.FromFile(pathTempImage);

更新

这是适用于我的代码:

void HandleFinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
    {
        _view.DismissModalViewControllerAnimated (true);

        BackgroundWorker bw = new BackgroundWorker();

        bw.DoWork += delegate(object bwsender, DoWorkEventArgs e2) {
            // determine what was selected, video or image
            bool isImage = false;
            switch(e.Info[UIImagePickerController.MediaType].ToString()) {
                case "public.image":
                    Console.WriteLine("Image selected");
                    isImage = true;
                    break;
                case "public.video":
                    Console.WriteLine("Video selected");
                    break;
            }

            // get common info (shared between images and video)
            NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl;
            if (referenceURL != null)
                Console.WriteLine("Url:"+referenceURL.ToString ());

            // if it was an image, get the other image info
            if(isImage) {
                // get the original image  
                originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
                if(originalImage != null) {
                    NSData data = originalImage.AsPNG();
                    _picture = new byte[data.Length];

                    ImageResizer resizer = new ImageResizer(originalImage);
                    resizer.RatioResize(200,200);

                    string path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
                    string pathTempImage = Path.Combine(path, "tempImage.png");
                    string filePath = Path.Combine(path, "OriginalImage.png");

                    NSData dataTempImage = resizer.ModifiedImage.AsPNG();
                    byte[] tempImage = new byte[dataTempImage.Length];

                    System.Runtime.InteropServices.Marshal.Copy(dataTempImage.Bytes,tempImage,0,Convert.ToInt32(tempImage.Length));

                    //OriginalImage
                    File.WriteAllBytes(filePath, _picture); 

                    //TempImag
                    File.WriteAllBytes(pathTempImage, tempImage);


                    UIImage image = UIImage.FromFile(pathTempImage);

                    _view.InvokeOnMainThread (delegate {
                        templateCell.BindDataToCell(appSelectPicture.Label, image);
                    });

                    _picture = null;
                }
            } else { // if it's a video
                // get video url
                NSUrl mediaURL = e.Info[UIImagePickerController.MediaURL] as NSUrl;
                if(mediaURL != null) {
                    Console.WriteLine(mediaURL.ToString());
                }
            }          
            // dismiss the picker
        };

        bw.RunWorkerAsync();

        bw.RunWorkerCompleted += HandleRunWorkerCompleted;
    }

1 个答案:

答案 0 :(得分:1)

byte[] tempImage = new byte[data.Length];
File.WriteAllBytes(pathTempImage, tempImage);

在保存之前,您没有将图像data复制到已分配的阵列。这导致大的空文件不是有效的图像。

尝试使用其中一个NSData.Save重载,例如:

NSError error;
data.Save (pathTempImage, NSDataWritingOptions.FileProtectionNone, out error);

这样可以避免分配byte[]数组。