How to check if an ALAsset still exists using a URL

前端 未结 4 898
悲哀的现实
悲哀的现实 2020-12-05 11:57

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...

<
相关标签:
4条回答
  • 2020-12-05 12:22

    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)
    }];
    
    0 讨论(0)
  • 2020-12-05 12:23

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 12:24

    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");
    }   
    
    0 讨论(0)
  • 2020-12-05 12:27

    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
    }
    
    0 讨论(0)
提交回复
热议问题