Getting the path of an ALAsset

前端 未结 3 453
温柔的废话
温柔的废话 2020-12-28 09:35

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         


        
3条回答
  •  不思量自难忘°
    2020-12-28 09:54

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

提交回复
热议问题