How to check if an ALAsset still exists using a URL

Mathieu picture Mathieu · Aug 28, 2011 · Viewed 11.2k times · Source

I've got an array containing ALAsset urls (not full ALAsset objects) So each time I start my application I want to check my array to see if it's still up to date...

So I tried

NSData *assetData = [[NSData alloc] initWithContentsOfFile:@"assets-library://asset/asset.PNG?id=1000000001&ext=PNG"];

but assetData is allways nil

thx for the help

Answer

Johnmph picture Johnmph · Aug 28, 2011

Use assetForURL:resultBlock:failureBlock: method of ALAssetsLibrary instead to get the asset from its URL.

// Create assets library
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];

// Try to load asset at mediaURL
[library assetForURL:mediaURL resultBlock:^(ALAsset *asset) {
    // If asset exists
    if (asset) {
        // Type your code here for successful
    } else {
        // Type your code here for not existing asset
    }
} failureBlock:^(NSError *error) {
    // Type your code here for failure (when user doesn't allow location in your app)
}];