How to check if an ALAsset still exists using a URL

眉间皱痕 提交于 2019-12-17 15:51:46

问题


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


回答1:


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



回答2:


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



回答3:


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
}



回答4:


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


来源:https://stackoverflow.com/questions/7221167/how-to-check-if-an-alasset-still-exists-using-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!