views:

31

answers:

2

I have an iPod Touch 2nd generation and I'm trying to retrieve a photo from the assets library by using the photo name. I ran restore on the device to make sure it is factory 4.1.

My header file has:

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


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

and my code is:

- (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);

...

Where photoFromURL is

- (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];
}

This runs fine on the simulator but the runloop never returns on the device. I get through the first three NSLog statements for fetching but the fourth never shows up.

Please help. - Dan

A: 

Location Services???

I found that I have to enable Location Services on the device. It never occurred to me that I need Location Services to access the photo library. I suppose they're all within the new assets library.

I had it turned off because iOS 4.0+ kills the iPod touch battery.

A: 

Haven't tested, but I can imagine you will need to add the storage attribute __block to the member variable fetching.

__block BOOL fetching;
epatel