我正在为iPad开发此应用程序。我的第一个屏幕将显示2个UIImageView(ImageView,ImageView2)和2个UIButton(确认,浏览)。
我将简要介绍一下我的应用程序的功能。 点击“浏览”按钮后,它将显示iPad照片库中的照片。选择照片后,照片库将被关闭,“ImageView”将显示所选照片。 点击“确认”按钮后,它将使用NSDocumentDirectory将图像存储到我的应用程序的项目中。之后,它将检索保存的照片并将其显示在“ImageView2”上。
浏览按钮..
- (IBAction) browsePhoto:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:CGRectMake(200,200,-100,-100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController release];
}
在ImageView上显示所选图像..
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
[self.popoverController dismissPopoverAnimated:YES];
imageView.image = selectedImage;
}
确认按钮..
- (IBAction) confirmPhoto:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:@"SavedImage.png"];
UIImage *image = imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
imageView2.image = [UIImage imageNamed:@"SavedImage.png"];
}
我可以查看照片库中的照片,选择照片,并使“ImageView”显示所选照片。但是当我点击“确认”按钮时,“ImageView2”不会显示照片。我不知道问题出在哪里。
照片甚至保存了吗? 或者我用来检索已保存照片的方式是错误的?
答案 0 :(得分:1)
NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,@"SavedImage.png"];
imageView2.image=[UIImage imageWithContentsOfFile:filePath];
答案 1 :(得分:0)
尝试使用以下方式加载图片:
[UIImage imageWithContentsOfFile:imageFilePath]