Runloop在模拟器上运行在设备上失败

时间:2010-10-25 17:26:05

标签: cocoa-touch ios nsrunloop

我有第二代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];
}

这在模拟器上运行良好,但是runloop永远不会在设备上返回。我通过前三个NSLog语句获取,但第四个从不显示。

请帮忙。 - 丹

2 个答案:

答案 0 :(得分:1)

位置服务???

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

我关闭它是因为iOS 4.0+杀死了iPod touch电池。

答案 1 :(得分:0)

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

__block BOOL fetching;