简体   繁体   English

如何从iPhone中的图片库中选择图像的路径?

[英]How to get path of the image choose from gallery in iPhone?

I have Five buttons.On each button action.I have open gallery and choose an image.But the problem is on each button action same path is coming. 我有五个按钮,每个按钮动作都有,我打开画廊并选择了一个图像,但是问题出在每个按钮动作上都出现了相同的路径。

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];

Set global variable initially int countVal = 1; 初始设置全局变量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

Hope this code is helpful for you 希望这段代码对您有帮助

I may be missing something, but it looks like you're using the same name for the image each time: Myimage.png. 我可能会丢失一些东西,但是看起来您每次都为图像使用相同的名称:Myimage.png。 Every time the image will be saved inside the documents directory with that same name. 每次图像将以相同名称保存在documents目录中。 Maybe add a tag to each button and use that to distinguish each of the 5 different images? 也许在每个按钮上添加一个标签,并使用它来区分5个不同的图像?

Try to use below code which just edited to you answer The problem is only with saving image name,try to save it with different name The below code will help you in saving image with diffrent name note: put the tag value to diffrent button(5 buttons) and call bello method in its target 尝试使用下面刚刚编辑给您的代码问题仅在于保存图像名称,请尝试使用其他名称保存。以下代码将帮助您保存具有不同名称的图像注意:将标记值放在diffrent按钮(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];

} }

by this you can save image with different name 这样您可以保存不同名称的图像

May be it will helpful for u 可能对您有帮助

[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];

OR 要么

Use this code 使用此代码

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);

If you still get it empty then try by checking if NSData is created properly 如果仍然为空,请尝试检查是否正确创建了NSData。

NSLog(@"webData.%@",webData);

OR 要么

Try this: I hope defnetly it will be be helpful to you 试试这个:我希望能对您有所帮助

- (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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM