Iphone 如何在2ndViewController中使用1stViewController中拍摄的图像?
用户可以选择是使用现有图像还是使用新图像作为背景 就FirstViewController而言,一切正常。遗憾的是,当显示SecondViewController时,图像不再设置为背景 如何启用图像作为SecondViewController的背景 我使用了这段代码(在SecondViewController中): 我试图将所选图像保存为“MyImage.png”,但它看起来好像不起作用。背景为黑色,而不是选定的图像 我使用此代码(来自1stViewController)将所选图片保存为“MyImage.png”: 一定有错误或逻辑错误。Iphone 如何在2ndViewController中使用1stViewController中拍摄的图像?,iphone,xcode,sdk,uiimagepickercontroller,Iphone,Xcode,Sdk,Uiimagepickercontroller,用户可以选择是使用现有图像还是使用新图像作为背景 就FirstViewController而言,一切正常。遗憾的是,当显示SecondViewController时,图像不再设置为背景 如何启用图像作为SecondViewController的背景 我使用了这段代码(在SecondViewController中): 我试图将所选图像保存为“MyImage.png”,但它看起来好像不起作用。背景为黑色,而不是选定的图像 我使用此代码(来自1stViewController)将所选图片保存为“MyI
谢谢你的帮助 尝试使用与保存图像的方式类似的代码加载图像,而不是使用
+imageNamed:
:构建完整路径并从那里加载图像
+imageNamed:
“在应用程序的主捆绑包中查找具有指定名称的图像。”(),但您不打算将其保存到该位置。我认为您无法写入应用程序的捆绑包,因此您将无法在此处使用+imageNamed:
。imageNamed通常用于加载作为应用程序资源一部分包含的图像
我怀疑你想要:
UIImage* image = [UIImage imageWithContentsOfFile:fileNameAndPath];
你的代码应该放在哪里?我不知怎么搞不懂。像那样?self.view.backgroundColor=[UIColor WithPatternImage:[UIImage imageWithContentsOfFile:@“Path?!”];那么,道路是什么呢?非常感谢。我有点困惑。根据您的代码,您的文件将存储在documentsDir中。即[UIColor COLOR WITH PATTERNAME:[UIImage IMAGEWITH CONTENTS OFFILE:[DocumentsDirectoryStringByAppendingPathComponent:imageName]];
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)anInfo {
//UIImage *selectedImage;
NSURL *mediaUrl;
mediaUrl = (NSURL *)[anInfo valueForKey:UIImagePickerControllerMediaURL];
if (mediaUrl == nil)
{
selectedImage = (UIImage *) [anInfo valueForKey:UIImagePickerControllerEditedImage];
if (selectedImage == nil)
{
selectedImage = (UIImage *) [anInfo valueForKey:UIImagePickerControllerOriginalImage];
NSLog(@"Original image picked.");
}
else
{
NSLog(@"Edited image picked.");
}
}
[picker dismissModalViewControllerAnimated:YES];
if (selectedImage != nil)
{
self.view.backgroundColor = [UIColor colorWithPatternImage:selectedImage];
}
// Get the image from the result
UIImage* ownImage = [anInfo valueForKey:UIImagePickerControllerOriginalImage];
// Get the data for the image as a PNG
NSData* imageData = UIImagePNGRepresentation(ownImage);
// Give a name to the file
NSString* imageName = @"MyImage.png";
// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
return;
}
UIImage* image = [UIImage imageWithContentsOfFile:fileNameAndPath];