2
尝试使用照片选择器选择图像并将该图像内部保存在应用程序文件夹中。将图像持久保存在应用程序内 - iOS
- (void) imagePickerController: (UIImagePickerController *) pickerdidFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToUse;
// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToUse = editedImage;
} else {
imageToUse = originalImage;
}
// Do something with imageToUse
//save it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageName = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"%d", myUniqueID]];
NSString *imagePath = [imageName stringByAppendingPathComponent:@".png"];
NSData *webData = UIImagePNGRepresentation(editedImage);
NSError* error = nil;
bool success = [webData writeToFile:imagePath options:NULL error:&error];
if (success) {
// successfull save
imageCount++;
[[NSUserDefaults standardUserDefaults] setInteger:imageCount forKey:@"imageCount"];
NSLog(@"@Success save to: %@", imagePath);
}
else if (error) {
NSLog(@"Error:%@", error.localizedDescription);
}
}
...
}
我无法弄清楚什么是writeToFile:::
回报false
但没有值在error
返回,所以我不能弄清楚怎么回事错了。任何帮助将不胜感激
感谢您的回复。做了改变..但仍然没有。最困惑的。 –
我不知道为什么......但改变 NSData * webData = UIImagePNGRepresentation(editedImage); 至 NSData * webData = [NSData dataWithData:UIImagePNGRepresentation(editedImage)]; 似乎已经工作 –
我不知道它为什么它没有工作的第一次,但我敢打赌,如果你改变这个'NSData'行,它仍然会工作... – Rob