0

我有第二代 iPod Touch,我正在尝试使用照片名称从资产库中检索照片。我在设备上运行恢复以确保它是出厂 4.1。

我的头文件有:

#import <AssetsLibrary/AssetsLibrary.h>
{
    BOOL fetching;
}
@property BOOL fetching;


typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);

我的代码是:

- (void) imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
        NSArray *array = [info allKeys];
        for (NSString *str in array) NSLog(@"Key = %@", str);

    // iPad uses UIImagePickerControllerMediaURL
    // iPhone uses UIImagePickerControllerReferenceURL

    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];

    fetching = NO;
    NSLog(@"1 fetching = %d", fetching);
    [self photoFromURL:url];
    NSLog(@"2 fetching = %d", fetching);
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    NSLog(@"3 fetching = %d", fetching);
    while (!fetching && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
    NSLog(@"4 fetching = %d", fetching);

...

photoFromURL 在哪里

- (void) photoFromURL:(NSURL *)inURL
{
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) 
        {
            photo = [UIImage imageWithCGImage:iref];
            [photo retain];
            fetching = YES;
       }
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Cannot get image - %@",[myerror localizedDescription]);
    };

    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:inURL 
                   resultBlock:resultblock
                  failureBlock:failureblock];
}

这在模拟器上运行良好,但运行循环永远不会在设备上返回。我通过前三个 NSLog 语句进行获取,但第四个从未出现。

请帮忙。- 丹

4

2 回答 2

1

位置服务???

我发现我必须在设备上启用定位服务。我从来没有想过我需要定位服务来访问照片库。我想它们都在新的资产库中。

我将其关闭是因为 iOS 4.0+ 会耗尽 iPod touch 电池。

于 2010-10-25T18:25:30.100 回答
0

尚未测试,但我可以想象您需要将 storage 属性添加__block到成员变量fetching

__block BOOL fetching;
于 2010-10-26T21:33:06.310 回答