如何从 iPhone 的图库中选择图像的路径?
Posted
技术标签:
【中文标题】如何从 iPhone 的图库中选择图像的路径?【英文标题】:How to get path of the image choose from gallery in iPhone? 【发布时间】:2013-12-16 00:34:01 【问题描述】:我有五个按钮。在每个按钮操作上。我打开图库并选择一个图像。但问题是每个按钮操作都出现相同的路径。
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
NSData* imageData = UIImagePNGRepresentation(image);
NSString * imageName = @"Myimage.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(@"fullPathToFile=%@",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
【问题讨论】:
用不同的名字保存图片 【参考方案1】:尝试使用下面刚刚编辑给您答案的代码 问题只是保存图像名称,尝试用不同的名称保存 下面的代码将帮助您保存具有不同名称的图像 注意:将标签值放入不同的按钮(5个按钮)并在其目标中调用bello方法
-(void)saveImage:(UIButton*)sender
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
NSData* imageData = UIImagePNGRepresentation(image);
NSString * imageName =[NSString stringWithFormat:@"Myimage%d.png",sender.tag];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(@"fullPathToFile=%@",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
这样你可以用不同的名字保存图片
【讨论】:
【参考方案2】:初始设置全局变量 int countVal = 1;
NSString * imageName = [NSString stringWithformat:@"%d.png",countVal]; // Imgae_name set here
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(@"fullPathToFile=%@",fullPathToFile); //important line
[imageData writeToFile:fullPathToFile atomically:NO];
count +=1; // Increment count here
【讨论】:
【参考方案3】:可能对你有帮助
[picker dismissModalViewControllerAnimated:YES];
imageView.image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:png];
[webData writeToFile:localFilePath atomically:YES];
NSLog(@"localFilePath.%@",localFilePath);
UIImage *image = [UIImage imageWithContentsOfFile:localFilePath];
或
使用此代码
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
imageView.image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:savedImagePath atomically:NO];
NSLog(@"localFilePath.%@",localFilePath);
如果你仍然把它弄空,那么请尝试检查 NSData 是否正确创建
NSLog(@"webData.%@",webData);
或
试试这个:我希望它对你有帮助
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
//you can use UIImagePickerControllerOriginalImage for the original image
//Now, save the image to your apps temp folder,
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload-image.tmp"];
NSData *imageData = UIImagePNGRepresentation(img);
//you can also use UIImageJPEGRepresentation(img,1); for jpegs
[imageData writeToFile:path atomically:YES];
//now call your method
[self uploadMyImageToTheWebFromPath:path];
【讨论】:
【参考方案4】:我可能遗漏了一些东西,但看起来您每次都使用相同的图像名称:Myimage.png。每次图像都会以相同的名称保存在文档目录中。也许为每个按钮添加一个标签并使用它来区分 5 个不同的图像中的每一个?
【讨论】:
以上是关于如何从 iPhone 的图库中选择图像的路径?的主要内容,如果未能解决你的问题,请参考以下文章