gpt4 book ai didi

ios - .enumerateGroupsWithTypes block 停止参数 Swift (Xcode 6 beta 5)

转载 作者:搜寻专家 更新时间:2023-10-31 08:19:14 30 4
gpt4 key购买 nike

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

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

...

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

...

})

这在 Swift (Xcode 6 beta 1) 中工作得很好。但是现在,我得到 2 个错误:

  1. " 'UnsafeMutablePointer' is not a subtype of 'error type' "

  2. " Use of undeclared type 'CMutablePointer' "

很明显 CMutablePointer 不存在了,所以我尝试修改停止参数,如:

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

这样改之后,第二个报错明显消失了,但是第一个改成了:

" Could not find an overload for 'init' that accepts the supplied arguments "

我什至尝试按照 this post 的建议将 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)

})

最佳答案

这是一个适合我的工作示例:此代码查找相册“projectname”并保护该相册“中”的图像。如果相册不存在,将创建相册。

注意:如果有同名专辑。您不能再次使用此名称创建相册。您必须使用新名称。在此示例中,项目名称将扩展到日期和时间。

顺便说一句,Apple 的应用程序可以创建同名相册。

    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

关于ios - .enumerateGroupsWithTypes block 停止参数 Swift (Xcode 6 beta 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147817/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com