如何从照片库加载照片并将其存储到应用程序项目中?

Llo*_*rth 12 iphone xcode objective-c ipad ios

我正在开发一个与我在这里找到的示例代码几乎相同的应用程序.但我想做的方法与示例代码不同.
链接:PhotoLocations

第一个屏幕将有一个ImageView和2个按钮(选择照片,确认).当点击"选择照片"按钮时,它将导航到另一个屏幕,该屏幕从我的iPad照片库中检索照片.选择照片后,它将关闭当前屏幕并返回到在ImageView上显示所选照片的​​第一个屏幕.

点击"确认"按钮后,照片将存储到我的应用程序项目中(例如/resources/images/photo.jpg).

我可以知道我该怎么办?

Ank*_*ava 32

这将带您进入图库,您可以选择图像.

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

这将有助于您选择图像

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    // Dismiss the image selection, hide the picker and

    //show the image view with the picked image

    [picker dismissViewControllerAnimated:YES completion:nil];
    //UIImage *newImage = image;
}
Run Code Online (Sandbox Code Playgroud)

然后您可以将此图像存储到文档目录中...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
Run Code Online (Sandbox Code Playgroud)

要单击图像,请使用此选项

- (IBAction) takePhoto:(id) sender
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imagePickerController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)