How do I know an NSURL of ALAsset in assets library exists?

2.3k views Asked by At

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)
1

There are 1 answers

3
Shyam Dixit On

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