2012-06-07 72 views
0

我已經通過UIImagePickerController完成了一個圖片選擇器。由於內存問題,我想將所選圖像保存到光盤,如果需要從文件路徑加載它。但我無法實現它的工作。將UIImage保存到個人文件夾,然後通過UIImage.FromFile加載它

如果我直接綁定原始圖像,它顯示沒有問題。

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
byte[] tempImage = new byte[data.Length]; 
File.WriteAllBytes(pathTempImage, tempImage); 

你不保存之前的圖像data複製到分配的數組。這導致一個大的空文件不是有效的圖像。

嘗試使用NSData.Save重載之一,如:

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

這將讓你避免分配byte[]陣列。

+0

因爲我無法管理,使之與data.Save我用System.Runtime.InteropServices.Marshal.Copy填充陣列。如果你有更多的信息,我必須使用NSData.Save,我會再次感謝:)所以,感謝你帶我走向正確的方向! –

相關問題