我尽我所能,但我被困在这里。我想从iPhone相册中导入所有照片。所以我想出了这个ALAssestsLibrary API。
photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: { group in
if group != nil
{
}
}, failureBlock: { error in println("\(error)")})
如何添加这行代码。
group enumerateAssetsUsingBlock:groupEnumerAtion
我尝试添加此功能,但它没有显示任何enumerateAssetsUsingBlock属性。?
这是实际的代码。 !在Objective-C
dispatch_async(dispatch_get_main_queue(), ^
{
@autoreleasepool
{
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"error occour =%@", [myerror localizedDescription]);
};
ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result!=NULL)
{
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
{
[self.g_imageArray addObject:result];
}
}
};
ALAssetsLibraryGroupsEnumerationResultsBlock
libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop)
{
if (group == nil)
{
return;
}
if (group!=nil) {
[group enumerateAssetsUsingBlock:groupEnumerAtion];
}
[self updatephotoList];
};
self.library = [[ALAssetsLibrary alloc] init];
[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:libraryGroupsEnumeration
failureBlock:failureblock];
}
});
答案 0 :(得分:8)
您的enumerationBlock
和failureBlock
的类型不正确。
例如,枚举块定义为
typealias ALAssetsLibraryGroupsEnumerationResultsBlock = (ALAssetsGroup!, UnsafeMutablePointer<ObjCBool>) -> Void
表示该参数是一个以(ALAssetsGroup!, UnsafeMutablePointer<ObjCBool>)
为参数并返回Void
的闭包:
{
(group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
// ...
}
所以你的代码应该是这样的:
photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos),
usingBlock: {
(group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if group != nil {
group.enumerateAssetsUsingBlock({
(asset: ALAsset!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
// ...
})
}
},
failureBlock: {
(myerror: NSError!) -> Void in
println("error occurred: \(myerror.localizedDescription)")
})
由于Swift的“自动类型推断”功能,你也可以这样写 如
photoLibrary.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos),
usingBlock: {
group, stop in
if group != nil {
group.enumerateAssetsUsingBlock({
asset, index, stop in
// ...
})
}
},
failureBlock: {
myerror in
println("error occurred: \(myerror.localizedDescription)")
})
但在这种情况下,第一个版本可能更容易理解。
答案 1 :(得分:0)
请注意,自iOS 9.0起不推荐使用该API:
快捷键5
let library = ALAssetsLibrary()
library.enumerateGroups(withTypes: ALAssetsGroupType(ALAssetsGroupSavedPhotos), using: { (group, stop) -> Void in
if let group = group {
group.enumerateAssets({
(asset, index, stop) -> Void in
// ...
})
}
},
failureBlock: { myerror in
print("error occurred: \(myerror)")
})