iPhone:无法使用 AlAssetsLibrary 显示照片
我目前正在将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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此外,在 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 yourALAssetsLibrary
instance like in your code just after calling[library enumerateGroupsWithTypes:…]
all the collected assets will be invalid.See also the
ALAssetsLibrary
doc - overview:是的,这非常令人沮丧,但事实就是如此,您无法在代码中启用位置服务(不过这是一件好事)。
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).
我将通过
[[<#block#>> 将第一个块
。为什么?因为如果有很多资产需要枚举,这个块会被runloop自动释放。^assetGroupEnumerator
移动到堆中复制]自动释放]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.还有一件事:不要使用 [self loadAssetToScrollView:assets];在块内,但在块之前获取 self 的弱引用,如下所示:
并进一步在块内使用此weakSelf 实例:
为什么?以避免循环引用。
One more thing: don't use [self loadAssetToScrollView:assets]; inside the block but get the weak reference of self before the block like this:
and further use this weakSelf instance inside the block:
Why? To avoid retain cycles.