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

Iphone 如何从照片库加载照片并将其存储到应用程序项目中?,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,我正在开发一个与我在这里找到的示例代码几乎相同的应用程序。但是我想使用的方法与示例代码不同。 链接: 第一个屏幕将有一个ImageView和两个按钮(选择照片,确认)。 当点击“选择照片”按钮时,它将导航到另一个屏幕,从我的iPad照片库中检索照片。选择照片后,它将关闭当前屏幕,并返回到在ImageView上显示所选照片的第一个屏幕 当点击“确认”按钮时,照片将存储到我的应用程序项目中(例如/resources/images/photo.jpg) 我可以知道怎么做吗 UIImagePickerC

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

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

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

我可以知道怎么做吗

UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init];
cardPicker.allowsEditing=YES;
cardPicker.delegate=self;
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:appDelegate.cardPicker animated:YES];
[cardPicker release];
此委托方法将返回u从相册中选择的图像

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    noImage.image=img;
}

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

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;
}
然后您可以将此图像存储到文档目录

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];
}
-(iAction)上传照片:(id)发件人{
UIActionSheet*actionSheet=[[UIActionSheet alloc]initWithTitle:@“请选择您的选项”委托:自取消按钮:@“取消”破坏性按钮:无其他按钮:@“相机”,“画廊”,无];
[actionSheet showInView:self.view];
UITableViewCell*单击单元格=(UITableViewCell*)[[sender superview]superview];
NSIndexPath*clickedButtonPath=[jobstable indexPathForCell:clickedCell];
isSectionIndex=单击按钮路径节;
isRowIndex=单击按钮路径.row;
}
-(无效)操作表:(UIActionSheet*)操作表未使用buttonIndex(集成器)buttonIndex解除{
NSLog(@“来自didDismissWithButtonIndex-所选选项:%@,[actionSheet ButtonTitleIndex:buttonIndex]);
NSString*图像=[actionSheet buttonTitleAtIndex:buttonIndex];
如果([[actionSheet buttonTitleAtIndex:buttonIndex]IsequalString:@“Camera”]){
如果(![UIImagePickerController是源类型可用:UIImagePickerController源类型照相机])
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:nil消息:@“设备摄像头未工作”委托:nil cancelButtonTitle:nil otherButtonTitles:@“确定”,nil];
[警报显示];
返回;
}
否则{
UIImagePickerController*picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.allowsdediting=是;
picker.sourceType=UIImagePickerController源类型摄像头;
[自我呈现视图控制器:选择器动画:是完成:空];
}
}
如果([[actionSheet buttonTitleAtIndex:buttonIndex]IsequalString:@“Gallery”]){
UIImagePickerController*pickerView=[[UIImagePickerController alloc]init];
pickerView.allowsEditing=是;
pickerView.delegate=self;
[pickerView设置源类型:UIImagePickerController源类型SavedPhotosAlbum];
[自我呈现视图控制器:pickerView动画:是完成:无];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息{
UIImage*orginalImage=[info objectForKey:UIImagePickerController原始图像];
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:isRowIndex-inSection:isSectionIndex];
UITableViewCell*cell=[jobstable cellForRowAtIndexPath:indexPath];
UIImageView*tableIMAGE=(UIImageView*)[cell.contentView视图带标记:19];
tableIMAGE.image=orginalImage;
answersARRAY[indexPath.row]=[NSString stringWithFormat:@“-1,%@,%@,”,answersARRAY[indexPath.row],imageStris];
[自我解除视图控制器激活:是完成:无];
}
-(无效)ImagePickerController IDCancel:(UIImagePickerController*)选择器{
[picker DismissViewControllerInitiated:是完成:NULL];

}
在iPhone中,您必须使用UIImagePickerController从图库中检索图像,您可以通过将图像保存在NSDocumentsDirectoryYes中将其保存到您的应用程序中。谢谢,但我更希望有一些示例代码,以便我可以参考。google“UIImagePickerController参考”。转到苹果的网站。noImage是UIImageView,我在其中显示从相册中选取的图像…我可以知道如何将noImage存储到文档目录吗?我可以知道如何将图像从ImageView存储到文档目录吗?我刚刚想到了一个额外的功能。在第一个屏幕中,我将“选择照片”按钮更改为“从照片库浏览”,并添加一个新按钮“拍摄新照片”。点击该按钮,我的应用程序将启动摄像头,一旦用户拍照,它将返回到ImageView上显示图片的第一个屏幕,该屏幕也可以保存到我的文档目录中。。上面写着“你自己点击图片就用这个”存储部分对我不起作用。我试着把另一个名为“ImageView2”的imageview放在旁边。然后在我点击“确认”按钮后,我添加了额外的代码:ImageView2.image=[UIImage imagename:@“savedImage.png”];但它不显示图像!:([UIImage ImageName:@“savedImage.png”];这仅适用于存储在应用程序包中的图像..要从文档目录访问图像,您需要以下内容…NSArray*syspath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*docDirectory=[syspath objectAtIndex:0];NSString*filePath=[NSString stringWithFormat:@“%@/Thumbnail small.jpg”,docDirectory];background.image=[[UIImage alloc]initWithContentsOfFile:filePath]autorelease];此