初歩的な質問で大変恐縮なのですが、
web上で参考にしたページにある、functionの呼び出し方が分からず困っております。
https://qiita.com/m_orishi/items/0f8636151c3f98d9271e
【やりたいこと】
1.画像をファイル名を指定して保存
2.ファイル名をリストとして保持
3.forを使って順番に画像を呼び出し
2,3はなんとなくわかるのですが、1が分かりません。
imageIDというものが返されるのでそれがパスになるのかな?と思っているのですが、
そもそもsaveImageFuncの呼び出し方が分からず大変困っております。
どなたかお力を貸してください。
func saveImageFunc(_ imageData: Data, toAlbum albumName: String, completionBlock: @escaping (String?) -> Void, failureBlock: @escaping (Error?) -> Void) { let TempFilePath = "(NSTemporaryDirectory())temp.jpg" var imageID: String? = nil findOrCreatePhotoAlbum(name: albumName) { (album, error) in // 画像データを一時ファイルとして保存 let fileURL = URL(fileURLWithPath: TempFilePath) try? imageData.write(to: fileURL, options: .atomic) if let album = album, FileManager.default.fileExists(atPath: TempFilePath) { PHPhotoLibrary.shared().performChanges({ let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: fileURL)! let albumChangeRequest = PHAssetCollectionChangeRequest(for: album) let placeHolder = assetRequest.placeholderForCreatedAsset albumChangeRequest?.addAssets([placeHolder!] as NSArray) imageID = assetRequest.placeholderForCreatedAsset?.localIdentifier }) { (isSuccess, error) in if isSuccess { // 保存した画像にアクセスする為のimageIDを返却 completionBlock(imageID) } else { failureBlock(error) } _ = try? FileManager.default.removeItem(atPath: TempFilePath) } } else { failureBlock(error) _ = try? FileManager.default.removeItem(atPath: TempFilePath) } } } private func findOrCreatePhotoAlbum(name: String, completion: @escaping (PHAssetCollection?, Error?) -> Void) { var assetCollection: PHAssetCollection? var assetCollectionPlaceholder: PHObjectPlaceholder? let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "title = %@", name) let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions) if collection.firstObject != nil { assetCollection = collection.firstObject completion(assetCollection, nil) } else { PHPhotoLibrary.shared().performChanges({ let createRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name) assetCollectionPlaceholder = createRequest.placeholderForCreatedAssetCollection }) { (isSuccess, error) in if isSuccess { let refetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [assetCollectionPlaceholder!.localIdentifier], options: nil) assetCollection = refetchResult.firstObject completion(assetCollection, nil) } else { completion(nil, error) } } } } //画像の取得 func getImageData(id: String, completionBlock: @escaping (Data?) -> Void) { if let asset = PHAsset.fetchAssets(withLocalIdentifiers: [id], options: nil).firstObject { PHImageManager.default().requestImageData(for: asset, options: nil) { (data, _, _, _) in completionBlock(data) } } }
あなたの回答
tips
プレビュー