LoginSignup
8
8

More than 5 years have passed since last update.

assetのビデオをアプリ内のディレクトリにコピーする方法

Posted at

asset-library:// にあるビデオをアプリケーション内のディレクトリ(NSDocumentDirectory)とかにコピーしたいこととかありますよね。この場合、NSFileManagerではダメです。


ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        Byte *buffer = (Byte*)malloc(rep.size);
        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
        NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
        [data writeToURL:outputURL atomically:YES];
    } failureBlock:^(NSError *error) {
        NSLog(@"%@", [error description]);
    }];


画像の場合はこんな感じかな?


ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        CGImageRef cgImg = [rep fullResolutionImage];
        UIImage *img = [UIImage imageWithCGImage:cgImg];
        NSData *data = UIImagePNGRepresentation(img);
        [data writeToURL:outputURL atomically:YES];
    } failureBlock:^(NSError *error) {
        NSLog(@"%@", [error description]);
    }];


8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8