简体   繁体   English

如何使用IOS中的“照片”框架在相册中添加图像阵列?

[英]How do I add an array of images in an album using Photos framework in IOS?

I'm trying to integrate DKImagePicker into my app, because it simply looks better. 我正在尝试将DKImagePicker集成到我的应用中,因为它看起来更好。 When the user selects an album I want the app to save the images the user chose, in a new folder that I've created. 当用户选择相册时,我希望应用程序将用户选择的图像保存到我创建的新文件夹中。 Here is how I'm attempting to handle the save: 这是我尝试处理保存的方法:

...

@IBAction func openAlbum(sender: AnyObject) {

  // pickerController is  DKImagePickerController 

  pickerController.didSelectAssets = { [unowned self] (asset: [DKAsset]) in
    print("didSelectAssets")
    self.transferSelectedAssetsToAlbum(asset)
  }
  self.presentViewController(pickerController, animated: true, completion: nil)
}

func transferSelectedAssetsToAlbum(assets: [DKAsset]) {
  PHPhotoLibrary.sharedPhotoLibrary().performChanges({

  // Here I want to save all items in assets in the album
  for asset in assets {
    let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(asset.fullScreenImage!)
    let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
    let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
    albumChangeRequest?.addAssets([assetPlaceholder])
  }

  }, completionHandler: {
    success, error in
    print(success)
    print(error)
  })
} 

...

When running this Xcode, it complains saying 'NSFastEnumeration' cannot be used with array litteral . 运行此Xcode时,它抱怨说'NSFastEnumeration' cannot be used with array litteral I understand this is because of albumChangeRequest?.addAssets([assetPlaholder]) should not be under for .. in loop. 我了解这是因为albumChangeRequest?.addAssets([assetPlaholder])不应在for .. in loop下。 But, I want to save every image in the assets? 但是,我要保存资产中的每张图片吗? How can I make it work without a for loop? 没有for循环,如何使它工作?

Hopefully, I made that clear enough. 希望我已经足够清楚了。 If not please let me know so I can clarify my question. 如果没有,请告诉我,以便我澄清问题。

Apparently the error is misleading. 显然,该错误具有误导性。 All that was missing to make this work is ! 做这项工作所缺少的就是! for assetPlaceholder . 对于assetPlaceholder So all I did was replace albumChangeRequest?.addAssets([assetPlaceholder]) with albumChangeRequest?.addAssets([assetPlaceholder!]) 所以我albumChangeRequest?.addAssets([assetPlaceholder])albumChangeRequest?.addAssets([assetPlaceholder!])替换albumChangeRequest?.addAssets([assetPlaceholder]) albumChangeRequest?.addAssets([assetPlaceholder!])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM