Question

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

Was it helpful?

Solution

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

OTHER TIPS

Having assets path you can use this function to check if image exist:

-(BOOL) imageExistAtPath:(NSString *)assetsPath
{  
    __block BOOL imageExist = NO; 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:[NSURL URLWithString:assetsPath] resultBlock:^(ALAsset *asset) {
        if (asset) {
            imageExist = YES;
            }
    } failureBlock:^(NSError *error) {
        NSLog(@"Error %@", error);
    }];
    return imageExist;
}

Remember that checking if image exist is checking asynchronyus. If you want to wait until new thread finish his life call function "imageExistAtPath" in main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [self imageExistAtPath:assetPath];
});

Or you can use semaphores, but this is not very nice solution:

-(BOOL) imageExistAtPath:(NSString *)assetsPath
{  
    __block BOOL imageExist = YES; 
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);   
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);   
    dispatch_async(queue, ^{  
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:[NSURL URLWithString:assetsPath] resultBlock:^(ALAsset *asset) {
            if (asset) {
                dispatch_semaphore_signal(semaphore); 
            } else {
                imageExist = NO;
                dispatch_semaphore_signal(semaphore); 
                }
        } failureBlock:^(NSError *error) {
             NSLog(@"Error %@", error);
        }];
    });
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); 
    return imageExist;
}

For iOS 8 or later, there is a synchronous method to check if an ALAsset exists.

@import Photos;

if ([PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil].count) {
    // exist
}

Swift:

import Photos

if PHAsset.fetchAssetsWithALAssetURLs([assetURL], options: nil).count > 0 {
   // exist
}

Swift 3:

import Photos

if PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil).count > 0 {
   // exist
}

Use this method to check if file exists

NSURL *yourFile = [[self applicationDocumentsDirectory]URLByAppendingPathComponent:@"YourFileHere.txt"];

if ([[NSFileManager defaultManager]fileExistsAtPath:storeFile.path 
isDirectory:NO]) {

    NSLog(@"The file DOES exist");

} else {

    NSLog(@"The file does NOT exist");
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top