如何从照片库加载照片并将其存储到应用程序项目中?
Posted
技术标签:
【中文标题】如何从照片库加载照片并将其存储到应用程序项目中?【英文标题】:How to load photos from photo gallery and store it into application project? 【发布时间】:2011-09-30 04:06:12 【问题描述】:我正在开发一个与我在此处找到的示例代码几乎相同的应用程序。但我想做的方法与示例代码不同。 链接:PhotoLocations
第一个屏幕将有一个 ImageView 和 2 个按钮(选择照片、确认)。 当点击“选择照片”按钮时,它将导航到另一个屏幕,该屏幕从我的 iPad 的照片库中检索照片。选择照片时,它将解除当前屏幕并返回显示ImageView上所选照片的第一个屏幕。
点击“确认”按钮时,照片将存储到我的应用程序的项目中(例如 /resources/images/photo.jpg)。
请问我该怎么做?
【问题讨论】:
在 iPhone 中,您必须使用 UIImagePickerController 从图库中检索图像,您可以通过将其保存在 NSDocumentsDirectory 中将它们放入您的应用程序中 是的,谢谢,但我希望有一些示例代码,以便我参考。 谷歌“UIImagePickerController 参考”。转到 Apple 的网站。 【参考方案1】:这将带您进入图片库,您可以选择图片。
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];
【讨论】:
我可以知道如何将图像从 ImageView 存储到文档目录吗? 我只是想到了一个额外的功能。在第一个屏幕中,我将“选择照片”按钮更改为“从照片库浏览”,并添加一个新按钮“拍摄新照片”。通过点击该按钮,我的应用程序将启动相机,一旦用户拍照,它将返回到第一个屏幕,在 ImageView 上显示图片,该图片也可以保存到我的文档目录中。 最后一部分 .. 上面写着“要自己点击图片,请使用这个” 存储部分不适合我。我尝试在旁边放置另一个名为“ImageView2”的图像视图。然后在我点击“确认”按钮后,我添加了额外的代码:ImageView2.image = [UIImage imageNamed:@"savedImage.png"];但它不显示图像! :( [UIImage imageNamed:@"savedImage.png"];这仅适用于存储在应用程序包中的图像。要从文档目录访问图像,您需要以下内容... NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES ); NSString *docDirectory = [sysPaths objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/Thumbnail-small.jpg", docDirectory]; background.image = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];【参考方案2】:UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init];
cardPicker.allowsEditing=YES;
cardPicker.delegate=self;
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:appDelegate.cardPicker animated:YES];
[cardPicker release];
这个委托方法会返回你从相册中选择的图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
noImage.image=img;
【讨论】:
其中 noImage 是 UIImageView,我在其中显示从相册中挑选的图像... 我可以知道如何将 noImage 存储到文档目录吗?【参考方案3】:- (IBAction)UploadPhoto:(id)sender
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil];
[actionSheet showInView:self.view];
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell];
isSectionIndex = clickedButtonPath.section;
isRowIndex = clickedButtonPath.row;
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]);
NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"])
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
else
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"])
UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
[pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
[self presentViewController:pickerView animated:YES completion:nil];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex];
UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
tableIMAGE.image=orginalImage;
answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris];
[self dismissViewControllerAnimated:YES completion:nil];
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[picker dismissViewControllerAnimated:YES completion:NULL];
【讨论】:
这是对已被接受的旧问题的新答案。如果接受的答案没有,您的答案是什么?此外,添加有关此代码如何工作的说明将对未来的访问者有所帮助。以上是关于如何从照片库加载照片并将其存储到应用程序项目中?的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式从照片应用程序中检索所有照片/视频并将其复制到我的应用程序?