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

时间:2013-11-29 19:11:47

标签: ios iphone objective-c image ipad

我有五个按钮。每个按钮动作。我有一个打开的图库并选择一个图像。但是问题出现在每个按钮动作相同的路径上。

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

4 个答案:

答案 0 :(得分: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

希望此代码对您有所帮助

答案 1 :(得分:0)

我可能会遗漏一些东西,但看起来你每次都使用相同的名称:Myimage.png。每次将图像保存在具有相同名称的文档目录中。也许为每个按钮添加一个标签,并使用它来区分5个不同的图像?

答案 2 :(得分:0)

尝试使用下面编辑的代码来回答 问题仅在于保存图像名称,尝试使用不同的名称保存它 以下代码将帮助您使用不同的名称保存图像 注意:将标记值放入不同的按钮(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];

}

通过这个你可以保存不同名称的图像

答案 3 :(得分:0)

可能对你有帮助

[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

使用此代码

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

OR

试试这个:我希望它会对你有所帮助

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