Getting the path of an ALAsset

旧城冷巷雨未停 提交于 2020-01-20 17:07:48

问题


How can I get the path of each item in an array of ALAssets?

I would like to get the images so that I can add them to an email

e.g.

NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailViewController addAttachmentData:myData mimeType:@"image/png" fileName:@"sample"];

How can this be done?


回答1:


Assuming you already have access to an array of ALAsset objects, you can retrieve their URL like this:

someAsset.defaultRepresentation.url




回答2:


Assuming you have the Asset URL, such as assets-library://asset/asset.JPG?id=1000000477&ext=JPG:

      ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
      {
             // [[myasset defaultRepresentation] fullResolutionImage]
             // is a CGImageRef so you can process it like you would any CGImageRef to save to disk, resize, etc. 

                NSURL *urlPath = [[NSURL fileURLWithPath:[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]] URLByAppendingPathComponent:@"somefile.png"];

                CGImageDestinationRef ref = CGImageDestinationCreateWithURL((CFURLRef)urlPath, kUTTypePNG, 1, NULL);
                CGImageDestinationAddImage(ref, (CGImageRef)[[myasset defaultRepresentation] fullResolutionImage], NULL);

                NSDictionary *props = [[NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithFloat:1.0], kCGImageDestinationLossyCompressionQuality,
                                        nil] retain];

                CGImageDestinationSetProperties(ref, (CFDictionaryRef) props);

                CGImageDestinationFinalize(ref);
                CFRelease(ref);

        };

        NSURL *asseturl = [NSURL URLWithString:mediaurl];
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];

        NSString *asseturl = @"assets-library://asset/asset.JPG?id=1000000477&ext=JPG";

        [assetslibrary assetForURL:asseturl 
                       resultBlock:resultblock
                      failureBlock:^(NSError *error) {
                          NSLog(@"error couldn't get photo");
                      }]; 



回答3:


If you have an array of ALAssets then you can load the asset data.

for (ALAsset *asset in assetsArray)
{
    // You cannot use ALAsset URL for file access in NSFileManager or NSData.
    // Get asset data. But be careful with very large data:
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    unsigned long repSize      = (unsigned long)rep.size;

    Byte *buffer      = (Byte *)malloc(repSize);
    NSUInteger length = [rep getBytes:buffer fromOffset:0 length:repSize error:nil];

    NSData *myData = [NSData dataWithBytesNoCopy:buffer length:length freeWhenDone:YES];


    // fileName parameter in addAttachmentData:mimeType:fileName: can be any string.
    // You can take asset file name:
    NSString *fileName = [rep filename];

    // Then use in call:
    [mailViewController addAttachmentData:myData mimeType:@"image/png" fileName:fileName];
}


来源:https://stackoverflow.com/questions/5047643/getting-the-path-of-an-alasset

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