iPhone使用AlAssetsLibrary与您的应用程序自动同步照片

时间:2011-04-13 11:52:24

标签: iphone image sync alasset photolibrary

我刚刚开始开发一个应用程序,我希望将用户照片库中存储的所有图像作为资源自动同步。

我可以获取存储为资产的图像的URL。

e.g. assets-library://asset/asset.JPG?id=1000000003&ext=JPG

现在,如何将此图像复制到我的应用程序的文档目录中,或者如何在NSDataUIImage中显示存储在资源中的图像?

先谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在UIImagePickerControllerDelegate委托实施

中使用以下代码
- (void) imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info {

    //obtaining saving path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"my_photo.png"];

    //extracting image from the picker and saving it
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imageData = UIImagePNGRepresentation(editedImage);
    [imageData writeToFile:imagePath atomically:YES];

}