###前提・実現したいこと
UIImagePickerControllerで撮影し、UITextFieldで指定したアルバムに写真を保存したいと思い、
https://iphone-app-tec.com/ios/camera.html
http://ja.stackoverflow.com/questions/17776/指定したアルバム内への画像の保存方法について
などを参考に下記のようにしてみたのですが
###発生している問題・エラーメッセージ
albumChangeRequest!.addAssets([assetPlaceholder])のところにContextual type 'NSFastEnumeration' cannot be used with array literalのエラーが出て解決方法がわかりません。
###該当のソースコード
import UIKit
import Photos
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@IBOutlet var cameraView : UIImageView! @IBOutlet var btnCameraStart : UIButton! @IBOutlet var btnSavePic : UIButton! @IBOutlet var btnAlbum : UIButton! @IBOutlet var titleTextField: UITextField! @IBOutlet var label : UILabel! override func viewDidLoad() { super.viewDidLoad() label.text = "Tap the [Start] to take a picture" } // カメラの撮影開始 @IBAction func cameraStart(_ sender : AnyObject) { let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.camera // カメラが利用可能かチェック if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){ // インスタンスの作成 let cameraPicker = UIImagePickerController() cameraPicker.sourceType = sourceType cameraPicker.delegate = self self.present(cameraPicker, animated: true, completion: nil) } else{ label.text = "error" } } // 撮影が完了時した時に呼ばれる func imagePickerController(_ imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { cameraView.contentMode = .scaleAspectFit cameraView.image = pickedImage } //閉じる処理 imagePicker.dismiss(animated: true, completion: nil) label.text = "Tap the [Save] to save a picture" } // 撮影がキャンセルされた時に呼ばれる func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.dismiss(animated: true, completion: nil) label.text = "Canceled" } // 写真を保存 @IBAction func savePic(_ sender : AnyObject) { let image:UIImage! = cameraView.image if image != nil { //保存確認 let alertController: UIAlertController = UIAlertController(title: "保存確認", message: "本当に保存してよいですか?", preferredStyle: .alert) //保存確認->cancel let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel) { action -> Void in print("Pushed CANCEL") } alertController.addAction(cancelAction) //保存確認->保存 let saveAction: UIAlertAction = UIAlertAction(title: "保存", style: .default) { action -> Void in print("save") //アルバム名を指定する let albumTitle = self.titleTextField.text var theAlbum: PHAssetCollection? // アルバムを検索 let result = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.any, options: nil) result.enumerateObjects({(object, index, stop) in if let theCollection = object as? PHAssetCollection, theCollection.localizedTitle == albumTitle { theAlbum = theCollection } }) // アルバムにイメージを保存 if let anAlbum = theAlbum { PHPhotoLibrary.shared().performChanges({ let createAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image!) let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset! let albumChangeRequest = PHAssetCollectionChangeRequest(for: anAlbum) albumChangeRequest!.addAssets([assetPlaceholder]) }, completionHandler: nil) print("save image into the Album.") } } alertController.addAction(saveAction) present(alertController, animated: true, completion: nil) } else{ label.text = "image Failed !" } } // 書き込み完了結果の受け取り func image(_ image: UIImage, didFinishSavingWithError error: NSError!, contextInfo: UnsafeMutableRawPointer) { print("1") if error != nil { print(error.code) label.text = "Save Failed !" } else{ label.text = "Save Succeeded" } } // アルバムを表示 @IBAction func showAlbum(_ sender : AnyObject) { let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.photoLibrary if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){ // インスタンスの作成 let cameraPicker = UIImagePickerController() cameraPicker.sourceType = sourceType cameraPicker.delegate = self self.present(cameraPicker, animated: true, completion: nil) label.text = "Tap the [Start] to save a picture" } else{ label.text = "error" } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
###試したこと
Contextual type 'NSFastEnumeration' cannot be used with array literalのエラーについて調べたのですが、エラーの意味がわからず、解決に至っていません。コードに間違っているところがあるのでしょうか。
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3 Xcode8.2.1です。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー