gpt4 book ai didi

ios - 如何在 Swift 中枚举 ALAssetsGroup

转载 作者:搜寻专家 更新时间:2023-10-31 22:00:54 26 4
gpt4 key购买 nike

我尽力了,但我被困在这里。我想导入 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];
}
});

最佳答案

你的 enumerationBlockfailureBlock没有正确的类型。比如枚举 block 定义为

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)")
})

但在这种情况下,第一个版本可能更容易理解。

关于ios - 如何在 Swift 中枚举 ALAssetsGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968996/

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