【问题标题】:Getting the path of an ALAsset获取 ALAsset 的路径
【发布时间】:2026-02-20 11:00:01
【问题描述】:

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

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

例如

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

如何做到这一点?

【问题讨论】:

    标签: ios iphone alasset


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果您有一个 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];
      }
      

      【讨论】:

        【解决方案3】:

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

        someAsset.defaultRepresentation.url

        【讨论】:

        • 好的不错1。但是我如何在下面的例子中使用它。将其添加到电子邮件中。
        • NSData *assetData = [NSData dataWithContentsOfURL:someAsset.defaultRepresentation.url];
        • 非常感谢。附件是怎么添加的? [mailViewController addAttachmentData:myData mimeType:@"image/png" fileName:@"sample"];如果是url的内容?
        • 我发现NSData *assetData = [NSData dataWithContentsOfURL:someAsset.defaultRepresentation.url]在iOS 5 beta 5下不起作用。
        • @MarkAdams :我在文档中搜索了PHAsset,想知道有没有办法在新的照片框架下获取图像/视频的网址?