我有五个按钮,每个按钮动作都有我打开图库并选择一个图像,但是问题出在每个按钮动作上都走了同样的路。

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

最佳答案

初始设置全局变量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


希望这段代码对您有帮助

10-08 07:26