获取 ALAsset 的路径

发布于 2024-10-18 06:38:51 字数 340 浏览 3 评论 0原文

如何获取 ALAsset 数组中每个项目的路径?

我想获取图像,以便将它们添加到电子邮件中,

例如

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

这怎么能做到呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你爱我像她 2024-10-25 06:38:51

假设您已经有权访问 ALAsset 对象数组,您可以像这样检索它们的 URL:

someAsset.defaultRepresentation.url

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

someAsset.defaultRepresentation.url

锦欢 2024-10-25 06:38:51

假设您有资产 URL,例如 asset-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");
                      }]; 

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");
                      }]; 
南七夏 2024-10-25 06:38:51

如果您有 ALAsset 数组,则可以加载资产数据。

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文