iPhone:无法使用 AlAssetsLibrary 显示照片

发布于 2024-12-21 14:29:07 字数 2125 浏览 2 评论 0原文

我目前正在将ipa发送给朋友进行测试。有趣的是,我的一位测试人员能够使用 iPhone 4 查看她运行 IOS 5 的手机上存储的照片。

另外 2 位测试人员:一位拥有 iPhone 4 (IOS 4.3.3) 和 iPhone 3GS (IOS 5.0.1)他们中的一些人看不到手机上存储的照片。

这些是我使用过的代码:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
    if(result != NULL) {
        //NSLog(@"See Asset: %@", @"ggg");
        [assets addObject:result];

    }
};

NSLog(@"location = %i      length = %i ", range->location, range->length );

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {

        NSRange  *datarange = malloc(sizeof(NSRange));

        range->total = [group numberOfAssets];
        datarange->location = [group numberOfAssets] - range->location - range->length;
        datarange->length = range->length;
        NSLog(@" total = %i", range->total);

        int location = [group numberOfAssets] - range->location - range->length;

        if (location < 0)
        {
            datarange->location = 0;
            datarange->length = [group numberOfAssets] - range->location;
        }

        NSIndexSet *indexset = [ [NSIndexSet alloc] initWithIndexesInRange:*datarange];

        [group enumerateAssetsAtIndexes:indexset options:NULL
                             usingBlock:assetEnumerator];

        [indexset release];
        free(datarange);

        [self loadAssetToScrollView:assets]; 

    }

};    

    [assets release];

    assets = [[NSMutableArray alloc] init];


    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:assetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }];

    [library release];

我看到有人说有关其他线程中的异步事物,但不知道情况是否如此。他说将dispatch_async放在枚举组块中。

有谁知道出了什么问题。

此外,一位使用 iOS 4.3.3 的测试人员可以在“常规”->“设置”下启用位置服务后显示他的照片。为什么我们必须启用它?我们可以在代码上启用它吗,因为这会给使用我们应用程序的用户带来很大的干扰。

I currently sending ipa to friends for testing. Funny thing is, one of my tester able view her photos stored on her phone which was running IOS 5 using iPhone 4.

Another 2 testers: one has iPhone 4 (IOS 4.3.3) , and iPhone 3GS (IOS 5.0.1) both of them can't see photos stored on their phone.

These are the code I have used:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
    if(result != NULL) {
        //NSLog(@"See Asset: %@", @"ggg");
        [assets addObject:result];

    }
};

NSLog(@"location = %i      length = %i ", range->location, range->length );

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {

        NSRange  *datarange = malloc(sizeof(NSRange));

        range->total = [group numberOfAssets];
        datarange->location = [group numberOfAssets] - range->location - range->length;
        datarange->length = range->length;
        NSLog(@" total = %i", range->total);

        int location = [group numberOfAssets] - range->location - range->length;

        if (location < 0)
        {
            datarange->location = 0;
            datarange->length = [group numberOfAssets] - range->location;
        }

        NSIndexSet *indexset = [ [NSIndexSet alloc] initWithIndexesInRange:*datarange];

        [group enumerateAssetsAtIndexes:indexset options:NULL
                             usingBlock:assetEnumerator];

        [indexset release];
        free(datarange);

        [self loadAssetToScrollView:assets]; 

    }

};    

    [assets release];

    assets = [[NSMutableArray alloc] init];


    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:assetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }];

    [library release];

I saw somebody say about asynchronous thing in some other threads but don't know is it the case. He say put dispatch_async in the enumerate group block.

Does anyone know what is wrong.

Additionally, one of tester with iOS 4.3.3 can show his photos after enabling location services under General->Setting. Why we have to enable it? Can we enabled it on code since it will be quite disturbing to the user who using our application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悍妇囚夫 2024-12-28 14:29:07

此外,在 iOS 5.x 上,只要您需要使用收集的资源,您就必须保留 ALAssetsLibrary 实例。当您在调用 [library enumerateGroupsWithTypes:…] 之后释放您的 ALAssetsLibrary 实例(如代码中所示)时,所有收集的资源都将无效。

另请参阅 ALAssetsLibrary 文档 - 概述:

“...从库实例返回的对象的生命周期与库实例的生命周期相关。...”

Also on iOS 5.x you must retain your ALAssetsLibrary instance so long as you need to work with the collected assets. When you release your ALAssetsLibrary instance like in your code just after calling [library enumerateGroupsWithTypes:…] all the collected assets will be invalid.

See also the ALAssetsLibrary doc - overview:

"… The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance. …"

心欲静而疯不止 2024-12-28 14:29:07

是的,这非常令人沮丧,但事实就是如此,您无法在代码中启用位置服务(不过这是一件好事)。

Yes, it is incredibly frustrating, but that is how it is, and you cannot enable location services in code (that is a good thing though).

紫﹏色ふ单纯 2024-12-28 14:29:07

我将通过 [[<#block#>> 将第一个块 ^assetGroupEnumerator 移动到堆中复制]自动释放]。为什么?因为如果有很多资产需要枚举,这个块会被runloop自动释放。

I would move the first block ^assetGroupEnumerator to the heap by [[<#block#> copy] autorelease]. Why? Because this block would be autoreleased by the runloop, if there are many assets need to be enumerated through.

笛声青案梦长安 2024-12-28 14:29:07

还有一件事:不要使用 [self loadAssetToScrollView:assets];在块内,但在块之前获取 self 的弱引用,如下所示:

__block YourExampleClassInstance *weakSelf = self;

并进一步在块内使用此weakSelf 实例:

[weakSelf loadAssetToScrollView:assets];
void (^assetGroupEnumerator)… = ^(ALAssetsGroup *group, BOOL *stop) {
…
};

为什么?以避免循环引用。

One more thing: don't use [self loadAssetToScrollView:assets]; inside the block but get the weak reference of self before the block like this:

__block YourExampleClassInstance *weakSelf = self;

and further use this weakSelf instance inside the block:

[weakSelf loadAssetToScrollView:assets];
void (^assetGroupEnumerator)… = ^(ALAssetsGroup *group, BOOL *stop) {
…
};

Why? To avoid retain cycles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文