如何从照片库加载照片并将其存储到应用程序项目中? [英] How to load photos from photo gallery and store it into application project?

查看:111
本文介绍了如何从照片库加载照片并将其存储到应用程序项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

链接: PhotoLocations

I'm working on an application that is almost the same as the sample codes i found here. But the method i want to do is different from the sample codes.
Link: PhotoLocations

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

The first screen will have an ImageView and 2 buttons (Choose Photo, Confirm). When the 'Choose Photo' button is tapped, it will navigate to another screen which retrieves photos from my iPad's photo gallery. When a photo is chosen, it will dismiss the current screen and return to the first screen displaying the chosen photo on the ImageView.

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

When the 'Confirm' button is tapped, the photo will be stored into my application's project (e.g. /resources/images/photo.jpg).

我可以知道我该怎么办?

May i know how can i do this?

推荐答案

这个将带您到图库,您可以选择图像。

This will take you to the image gallery and you can select the image.

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];

这将帮助您选择图像

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

然后你可以将这张图片存储到文件目录......

And then you can store this image to the documents directory...

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

点击图片自己使用

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

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

    [self presentModalViewController:imagePickerController animated:YES];
}

这篇关于如何从照片库加载照片并将其存储到应用程序项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆