1

If I kept a url of a photo image in the Photo Library (ALAsset) in my database for several days, how do I know this photo still exists in the Photo Library by it's NSURL?

I've tried this but it did not work:

// Have already obtained the asset object from ALAssetsLibrary
NSURL *assetURL = [[(ALAsset*)asset defaultRepresentation] url];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: [assetURL absoluteString]];
NSLog(@"exists: %d (%@)", exists, [assetURL absoluteString]);

>> exists: 0 (assets-library://asset/asset.PNG?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&ext=PNG)
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Xaree Lee
  • 3,188
  • 3
  • 34
  • 55

1 Answers1

0

Try this as your requirement. Check it

dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    __block BOOL flag=YES;
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:path resultBlock:^(ALAsset *asset) {
        if (asset==nil)
        {
            flag=NO;
        }
        else
        {
            flag=YES;
        }
            dispatch_group_leave(group);
        } failureBlock:^(NSError *error){
                NSLog(@"operation was not successfull!");
                dispatch_group_leave(group);
    }];
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_release(group);
Shyam Dixit
  • 2,325
  • 3
  • 19
  • 27
  • I did this implementation, but it was stuck at the line `dispatch_group_wait(group, DISPATCH_TIME_FOREVER); `. This method might be invoked when another object of ALAssetsLibrary was busy. I think it may need another thread or dispatch queue to handle it. Right? – Xaree Lee Sep 12 '13 at 06:04
  • 1
    I found [an answer using GCD semaphore](http://stackoverflow.com/questions/7234445/wait-for-assetforurl-blocks-to-be-completed) which solves my problem. – Xaree Lee Sep 12 '13 at 07:27
  • Nice question & efforts also. – Shyam Dixit Sep 12 '13 at 09:07