2016-12-05 127 views
0

我是Swift初學者,剛剛在我的應用程序中完成了我的照片相機功能。我現在的問題是,當我從相機拍攝照片時,它不會保存到我iPhone上的照片庫中。一切正常完美我可以拍照,但是當我登錄照片時,似乎沒有保存。 我已經檢查過類似的問題,但我還沒有找到正確的答案,我發現他們是添加按鈕直接訪問照片庫的人,但我不需要這樣的按鈕。我需要的唯一功能是拍照,當用戶點擊選擇的照片將其保存在照片中。如何在照片庫Swift中保存圖片?

到目前爲止,我已經使用這個:

class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate{ 



let imagePicker: UIImagePickerController! = UIImagePickerController() 

    override func viewDidLoad() { 

    super.viewDidLoad() 

    imagePicker.delegate = self 

    let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes)) 

    upSwipe.direction = .up 

    view.addGestureRecognizer(upSwipe) 

} 

和功能:

func handleSwipes(sender:UISwipeGestureRecognizer) { 

     if (sender.direction == .up){ 

      if (UIImagePickerController.isSourceTypeAvailable(.camera)){ 



       if UIImagePickerController.availableCaptureModes(for: .rear) != nil { 
        imagePicker.allowsEditing = false 
        imagePicker.sourceType = .camera 
        imagePicker.cameraCaptureMode = .photo 
        present(imagePicker,animated: true, completion: {}) 
     } 
      } 

下面是我得到我的iPhone:當用戶選擇使用的照片我只希望,照片本身將被保存在畫廊中,就像那樣簡單。 enter image description here

回答

0
... 
UIImageWriteToSavedPhotosAlbum(imageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil) 
... 



func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { 
    if error == nil { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } else { 
     let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } 
} 

您可以在項目中使用該功能

+0

但我不使用imageView.image(我不想在屏幕上顯示它們,這就是爲什麼我沒有ImageView的) – Dakata

+0

啊,那麼這個鏈接也許給你一些幫助[鏈接](http://stackoverflow.com/questions/33663396/save-image-as-is-in-photo-album-using-swift) –

3

任何東西之前,你需要有「隱私 - 照片庫的添加使用情況說明」你的info.plist,否則您的應用程序會崩潰

使用此:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{ 
      UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) 

      dismiss(animated: true, completion: nil) 
     } 
    } 

,然後添加以下功能(同樣的功能saltTigerK WRO TE)

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) { 
    if let error = error { 
     // we got back an error! 
     let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } else { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .default)) 
     present(ac, animated: true) 
    } 
} 

來源:https://www.hackingwithswift.com/read/13/5/saving-to-the-ios-photo-library

+0

爲什麼這不是公認的答案? –