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

时间:2014-08-05 20:34:06

标签: ios xcode swift

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

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

...

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

  ...

})

这在Swift(Xcode 6 beta 1)中完美无缺。但是现在,我得到了2个错误:

  1.   

    &#34; &#39; UnsafeMutablePointer&#39;不是&#39;错误类型的子类型&#39; &#34;

  2.   

    &#34;使用未申报的类型&#39; CMutablePointer&#39; &#34;

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

    ..., stop: UnsafeMutablePointer<ObjCBool> ...
    

    在此更改后,第二个错误显然消失了,但第一个错误转换为:

      

    &#34;无法找到&#39; init&#39;接受提供的参数&#34;

    我甚至尝试将UnsafeMutablePointer更改为UnsafePointer,如this post所示。

    修改

    以下是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)
    
        })
    

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

NSLog("Error!", nil)错误,应为NSLog("Error!")。 (这似乎混淆了Swift编译器并导致无关的错误消息。)