.enumerateGroupsWithTypes阻止停止参数Swift(Xcode 6 beta 5)

Phi*_*ler 4 xcode ios swift

今天我将Xcode 6升级到beta 5(来自beta 1),你可以想象我发现我之前完美运行的Swift应用程序充满了各种错误(好吧,从beta 1有很多变化).在所有错误中,有一个我无法弄清楚如何修复.它与swift闭包有关,特别是.enumerateGroupsWithTypes方法的enumerationBlock参数.这是代码:

assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup?, stop: CMutablePointer<ObjCBool>) in

...

}, failureBlock: {
  (error: NSError!) in

  ...

})
Run Code Online (Sandbox Code Playgroud)

这确实在Swift(Xcode 6 beta 1)中完美运行.但现在,我得到2个错误:

  1. "'UnsafeMutablePointer'不是'错误类型'的子类型"

  2. "使用未声明类型'CMutablePointer'"

很明显,CMutablePointer不再存在,所以我试图修改stop参数,如:

..., stop: UnsafeMutablePointer<ObjCBool> ...
Run Code Online (Sandbox Code Playgroud)

在这个改变之后,第二个错误显然消失了,但第一个转变为:

"无法找到接受所提供参数的'init'的重载"

我甚至尝试将UnsafeMutablePointer更改为UnsafePointer,如本文所述.

编辑:

以下是enumerateGroupsWithTypes方法的完整代码:

assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
    (group: ALAssetsGroup?, stop: UnsafeMutablePointer<ObjCBool>) in
    if group != nil {
    group!.setAssetsFilter(ALAssetsFilter.allPhotos())
    group!.enumerateAssetsAtIndexes(NSIndexSet(index: group!.numberOfAssets()-1), options: nil, usingBlock: {
      (result: ALAsset!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) in
      if result {
        var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation()
        url = alAssetRapresentation.url()
      }
      })
    }
    else if group == nil {

      assetLib.assetForURL(url, resultBlock: {
        (asset: ALAsset!) in
        if asset != nil {
        var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
        var iref = assetRep.fullResolutionImage().takeUnretainedValue()
        var image = UIImage(CGImage: iref)


        imageView.image = image

        self.view.addSubview(imageView)

          let mask = CAShapeLayer()
          mask.path = UIBezierPath(ovalInRect: CGRectMake(0, 0, 200, 200)).CGPath
          mask.frame = CGPathGetPathBoundingBox(mask.path)

          mapView.layer.mask = mask

          self.view.addSubview(mapView)

        }
        }, failureBlock: {
          (error: NSError!) in

          NSLog("Error!", nil)
        })
    }

    }, failureBlock: {
      (error: NSError!) in

      NSLog("Error!", nil)

    })
Run Code Online (Sandbox Code Playgroud)

小智 7

这是一个适用于我的工作示例:此代码查找专辑"projectname"并保护此相册中的图像"安全".如果相册不存在,则会创建相册.

注意:如果有一张具有相同名称的相册.您无法再次使用此名称创建相册.您必须使用新名称.在此示例中,projectname将按日期和时间进行扩展.

顺便说一句,Apples应用程序可以创建一个具有相同名称的相册.

    func saveImage(projectName : String) { // Return a new projectname  
                                      //  in the var self.newProjectName if the old one could not created
    if self.isSaved {  // was here bevor
        return
    }

    let library = ALAssetsLibrary()                 // This object will provide the access to to the library

    // List over all groups in the PhotoDirectory
    // ALAssetsGroupAll is the key to select the listed groups
    // possible change to type:Album
    library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAll),
        usingBlock: {(group : ALAssetsGroup!, stop : UnsafeMutablePointer<ObjCBool>) in
            if group != nil {                                   // The listing of the directory content has found an object
                // Did we search for this album?
                if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(projectName) {
                    stop.initialize(true)                       // Stop the enumeration thread
                    library.writeImageToSavedPhotosAlbum(self.cgImage, metadata: self.ciImage?.properties(),
                        completionBlock: {(assetUrl, error: NSError?) -> Void in
                        if let theError = error?.code {
                            lapApp.logger.addLog("saved image failed, first try \(error?.localizedDescription) code \(theError)")
                        } else {
                            library.assetForURL(assetUrl,
                                resultBlock: { (asset: ALAsset!) -> Void in
                                group.addAsset(asset)
                                self.isSaved = true
                                return // Stop this process and leave
                                }, failureBlock: {
                                    (theError: NSError!) -> Void in
                                    lapApp.logger.addLog("error occurred, image to album at the first try: \(theError.localizedDescription) ")
                            })
                        }
                    })
                    return // write image to the found album
                } else {
                 // Album not found, enumeration will continue
               }
            }
            else { // The album was not found, so we will create an album
                if stop.memory.boolValue {  // The enumeration will go over the end of the list. The stop-signal comes some time to late?
                    return
                }
                library.addAssetsGroupAlbumWithName(projectName,
                    resultBlock: {(group: ALAssetsGroup?) -> Void in
                    if let thegroup = group {         // Check for a name conflict, possible was a album with the same name deleted. IOS8 will not create this album!
                        // The album was correct created, now we will add the picture to the album
                        library.writeImageToSavedPhotosAlbum(self.cgImage, metadata: self.ciImage?.properties(), completionBlock: {
                            (assetUrl, error: NSError?) -> Void in
                            if let theError = error?.code {
                                lapApp.logger.addLog("save image in new album failed. \(error?.localizedDescription) code \(theError)")
                            } else {
                                library.assetForURL(assetUrl,
                                    resultBlock: { (asset: ALAsset!) -> Void in
                                    thegroup.addAsset(asset)
                                    self.isSaved = true
                                    stop.initialize(true)                       // Stop the enumeration thread
                                    return
                                    }, failureBlock: {
                                        (theError: NSError?) -> Void in
                                        lapApp.logger.addLog("error occurred: \(theError?.localizedDescription)")
                                })
                            }
                        })
                        return

                    } else {                       // Name conflic with a deleted album.
                                                   // Work around: Create the Album with the Projectname an extend the name with Date and time
                        let formatter : NSDateFormatter = NSDateFormatter()
                        formatter.dateFormat = "yy.MM.dd hh:mm:ss"
                        let extensionDate = formatter.stringFromDate(NSDate())
                        self.newProjectName = projectName + " " + extensionDate // This is the new projectname
                        library.addAssetsGroupAlbumWithName(self.newProjectName,
                            resultBlock: {(group: ALAssetsGroup?) -> Void in
                            if let theGroup = group {
                                library.writeImageToSavedPhotosAlbum(self.cgImage, metadata: self.ciImage?.properties(), completionBlock: {
                                    (assetUrl, error: NSError?) -> Void in
                                    if let theError = error {
                                       lapApp.logger.addLog("save image with new album name failed. \(error?.localizedDescription) code \(theError) \(self.newProjectName)")
                                    } else {
                                       library.assetForURL(assetUrl, resultBlock: { (asset: ALAsset!) -> Void in
                                        theGroup.addAsset(asset)
                                        self.isSaved = true
                                        stop.initialize(true)                       // Stop the enumeration thread
                                        return
                                        }, failureBlock: {
                                            (theError: NSError?) -> Void in
                                            lapApp.logger.addLog("error at write image in new album occurred: \(theError?.localizedDescription)")
                                        })
                                    }
                                })
                            } else {
                                lapApp.logger.addLog("Problem adding albums with the name \(self.newProjectName)")
                            }
                        },
                        failureBlock: {
                                (error:NSError?) -> Void in
                                lapApp.logger.addLog("Problem adding albums: \(error)")
                        })
                    }
                    },
                    failureBlock: {
                        (error:NSError?) -> Void in
                        lapApp.logger.addLog("Problem loading albums: \(error)")
                })
            }
        }, failureBlock: { (error:NSError?) in lapApp.logger.addLog("Problem loading albums: \(error)") })
} // End SaveImage
Run Code Online (Sandbox Code Playgroud)