11

ALAsset の URL (完全な ALAsset オブジェクトではない) を含む配列を取得したので、アプリケーションを起動するたびに、配列がまだ最新かどうかを確認する必要があります...

だから私は試しました

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

しかし、assetData は常に nil です

助けてくれてthx

4

4 に答える 4

21

URL からアセットを取得するには、代わりに ALAssetsLibrary の assetForURL:resultBlock:failureBlock: メソッドを使用します。

// 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)
}];
于 2011-08-28T16:45:40.530 に答える
7

アセット パスがあれば、この関数を使用して画像が存在するかどうかを確認できます。

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

イメージが存在するかどうかを確認することは、非同期を確認していることに注意してください。新しいスレッドが寿命を迎えるまで待ちたい場合は、メインスレッドで関数「imageExistAtPath」を呼び出します。

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

または、セマフォを使用することもできますが、これはあまり良い解決策ではありません:

-(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;
}
于 2012-10-08T13:39:52.430 に答える
0

このメソッドを使用して、ファイルが存在するかどうかを確認します

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");
}   
于 2016-12-27T04:23:31.030 に答える