2015-06-17 105 views
0

我有以下代碼拍攝照片並將其保存到相機膠捲。我需要能夠在那裏使用它,然後在保存之後不必回到畫廊並選擇它。保存照片並立即使用它

我還沒有找到任何有關如何做到這一點的例子。

@IBOutlet weak var imagePicked: UIImageView! 

    @IBAction func CameraBtn(sender: UIButton) { 
     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { 
      var imagePicker = UIImagePickerController() 
      imagePicker.delegate = self 
      imagePicker.sourceType = UIImagePickerControllerSourceType.Camera; 
      imagePicker.allowsEditing = false 
      self.presentViewController(imagePicker, animated: true, completion: nil) 
      cameraBool = true 
     } 
    } 

    @IBAction func GalleryBtn(sender: UIButton) { 
     println("gallery button clicked") 
     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { 
      var imagePicker = UIImagePickerController() 
      imagePicker.delegate = self 
      imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary; 
      imagePicker.allowsEditing = true 
      self.presentViewController(imagePicker, animated: true, completion: nil) 
      cameraBool = false 
     } 
    } 

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
     imagePicked.image = image 
     if (cameraBool){ 
      var imageData = UIImageJPEGRepresentation(imagePicked.image, 0.6) 
      var compressedJPGImage = UIImage(data: imageData) 
      UIImageWriteToSavedPhotosAlbum(compressedJPGImage, nil, nil, nil) 

     } 
     self.dismissViewControllerAnimated(true, completion: nil); 
    } 

我需要能夠在下面的Web服務功能,使用這個保存的圖像:

func urlRequestWithComponents(urlString:String, parameters:Dictionary<String, String>, imageData:NSData) -> (URLRequestConvertible, NSData) { 

     // create url request to send 
     var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!) 
     mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue 
     let boundaryConstant = "myRandomBoundary12345"; 
     let contentType = "multipart/form-data;boundary="+boundaryConstant 
     mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type") 



     // create upload data to send 
     let uploadData = NSMutableData() 

     // add image 
     uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) 
     uploadData.appendData("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) 
     uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) 
     uploadData.appendData(imageData) 

     // add parameters 
     for (key, value) in parameters { 
      uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) 
      uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!) 
     } 
     uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) 



     // return URLRequestConvertible and NSData 
     return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData) 
    } 

,當我從庫中選擇圖像,並把我的web服務工作,在我的ImageView上屏幕。但是當我拍攝一張照片時,我需要首先將它保存到圖庫中,但我缺少的是如何以編程方式從庫中引用此新創建的照片。

UPDATE: 我假定從NSURL轉換爲NSData的將如下面是從下方相機塊的檢索。注意當用戶選擇拍攝照片時,cameraBool設置爲true。 ImageChanged在imagePickerController功能

if (imageChanged){ 
     //changed from placeholder 
      if (!cameraBool){ 
       //retrieved from gallery 
       let image = pic 
       let imageData = UIImagePNGRepresentation(image) 
      } 
      else{ 
       //retrieved from camera 
       let imageData = NSData(contentsOfURL: cameraPath) 
      } 
     } 
     else{ 
      //upload nil 
      let imageData = nil 
     } 

我的問題是,圖片的路徑,我imagePickerController函數只被定義設置。所以我需要在文件的頂部聲明cameraPath,以便它可以在imagePickerController函數中使用,然後在我的上傳webservice中使用。看來我無法將cameraPath初始化爲零。所以我不知道如何初始化它?

var cameraPath : NSURL = nil 
//not allowed 

當我剛剛有:

var cameraPath : NSURL 

我得到我的控制器沒有initialisers

+0

你可以這樣說:'VAR cameraPath:NSURL ' –

回答

4

您可以通過相機獲得最近添加的圖像的路徑是這樣的:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
    imagePicked.image = image 
    if (cameraBool){ 
     var imageData = UIImageJPEGRepresentation(imagePicked.image, 0.6) 
     var compressedJPGImage = UIImage(data: imageData) 
     ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedJPGImage!.CGImage, orientation: ALAssetOrientation(rawValue: compressedJPGImage!.imageOrientation.rawValue)!, 
     completionBlock:{ (path:NSURL!, error:NSError!) -> Void in 
      println("\(path)") //Here you will get your path 
     }) 
    } 
    self.dismissViewControllerAnimated(true, completion: nil); 
} 

,它將打印路徑是這樣的:

assets-library://asset/asset.JPG?id=C5E0EB97-5D3D-41F9-8782-F48140144432&ext=JPG 
+0

我的函數上傳圖像期待的NSData類型,我將如何解決這個問題,因爲你的代碼似乎給我一個NSURL?我將添加我的功能上傳到我的問題 – user2363025

+0

所以,你想通過該URL將該圖像轉換爲nsdata? –

+0

是的,這將做的詭計我猜 – user2363025

0

您可以使用協議/代表團模式這個錯誤。請參閱Apple Doc

基本上,你需要在你的類中創建一個Protocol,並設置一個委託變量,然後把你的Image交給新的View Controller。你可以找到一個教程here

1

使用ALAssetsLibrary,如果使用UIImageWriteToSavedPhotosAlbum保存圖像,則不會獲得指向它的指針。更多info

編輯: 我認爲@DharmeshKheni的解決方案應該工作。我的猜測是你在設置之前閱讀cameraPath。換句話說:我想你會在保存之前嘗試使用新圖片。你能告訴cameraPath的設置,以及你讀的地方嗎?

+0

我的上傳圖片的函數期望一種NSData類型,我將如何解決這個問題,因爲鏈接中的代碼似乎給了我一個NSURL?我將添加我的功能上傳到我的問題 – user2363025