我刚刚开始开发一个应用程序,我希望将用户照片库中存储的所有图像作为资源自动同步。
我可以获取存储为资产的图像的URL。
e.g. assets-library://asset/asset.JPG?id=1000000003&ext=JPG
现在,如何将此图像复制到我的应用程序的文档目录中,或者如何在NSData
或UIImage
中显示存储在资源中的图像?
先谢谢。
答案 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];
}