2017-06-21 2 views
0

Das klingt vielleicht wiederholt, aber nach der Suche, während ich immer noch nicht die gewünschte Antwort für Swift 3 erhalten. Nach dem Erstellen eines Platzhalters für Asset beim Versuch, Assets mit addAssets() hinzuzufügen Methode Xcode schlägt mir vor, AssetPlacehoder zu FastEnumeration Typ zu werfen. Und wenn ich das mache, verursacht das eine Ausnahme. Kann mir jemand helfen?So erstellen Sie ein benutzerdefiniertes Album in Swift 3 iOS 10

+0

Können Sie die Ausnahme posten, die Sie erhalten? – bjtitus

+0

@ bjtitus, das ist die Ausnahme, die ich bekomme: '[Generic] Erstellen eines Bildformats mit einem unbekannten Typ ist ein Fehler Konnte Wert vom Typ 'PHAsset' (0x15e4a0578) nicht in 'NSFastEnumeration' (0x15d4d86e0) .' –

Antwort

0

Verwenden Sie diese Klasse und fragen Sie nach Erlaubnis, wenn Sie benötigen und bevor Sie ein Bild zum Album hinzufügen.

class CustomPhotoAlbum: NSObject { 

     static let albumName = "YourAlbumName" 
     static let sharedInstance = CustomPhotoAlbum() 

     var assetCollection: PHAssetCollection! 

     override init() { 
      super.init() 

      if let assetCollection = fetchAssetCollectionForAlbum() { 
       self.assetCollection = assetCollection 
       return 
      } 
     } 

     func requestAuthorizationHandler(status: PHAuthorizationStatus) { 
      if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized { 
       // ideally this ensures the creation of the photo album even if authorization wasn't prompted till after init was done 
       print("trying again to create the album") 
       self.createAlbum() 
      } else { 
       print("should really prompt the user to let them know it's failed") 
      } 
     } 

     func createAlbum() { 
      PHPhotoLibrary.shared().performChanges({ 
       PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomPhotoAlbum.albumName) // create an asset collection with the album name 
      }) { success, error in 
       if success { 
        self.assetCollection = self.fetchAssetCollectionForAlbum() 
       } else { 
        print("error \(String(describing: error))") 
       } 
      } 
     } 

     func fetchAssetCollectionForAlbum() -> PHAssetCollection? { 
      let fetchOptions = PHFetchOptions() 
      fetchOptions.predicate = NSPredicate(format: "title = %@", CustomPhotoAlbum.albumName) 
      let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions) 

      if let _: AnyObject = collection.firstObject { 
       return collection.firstObject 
      } 
      return nil 
     } 

    func save(image: UIImage , completion : @escaping() ->()) { 
      if assetCollection == nil { 
       return       // if there was an error upstream, skip the save 
      } 

      PHPhotoLibrary.shared().performChanges({ 
       let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image) 
       let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset 
       let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection) 
       let enumeration: NSArray = [assetPlaceHolder!] 

       if self.assetCollection.estimatedAssetCount == 0 
       { 
        albumChangeRequest!.addAssets(enumeration) 
       } 
       else { 
        albumChangeRequest!.insertAssets(enumeration, at: [0]) 
       } 

      }, completionHandler: { status , errror in 
       completion() 
      }) 
     } 
} 

Rufen Sie diese Methode auf, um das Bild zu speichern.

CustomPhotoAlbum.sharedInstance.save(image: YourImage, completion: { 

         DispatchQueue.main.async(execute: { 
          //"Any UI update should be in main thread." 
         }) 
        }) 
+0

Was ruft requestAuthorizationHandler auf? Es sollte aufgerufen werden, um das Fotoalbum an erster Stelle zu erstellen. – VagueExplanation

Verwandte Themen