Getting the path of an ALAsset

前端 未结 3 432
温柔的废话
温柔的废话 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 10:12

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

提交回复
热议问题