Iphone 在应用程序内持久保存图像-iOS

Iphone 在应用程序内持久保存图像-iOS,iphone,ios,image,ios6,data-persistence,Iphone,Ios,Image,Ios6,Data Persistence,正在尝试使用photo picker选择图像,并将该图像内部保存在“应用程序”文件夹中 - (void) imagePickerController: (UIImagePickerController *) pickerdidFinishPickingMediaWithInfo: (NSDictionary *) info { NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; UIImage

正在尝试使用photo picker选择图像,并将该图像内部保存在“应用程序”文件夹中

- (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
中没有返回值,所以我不能理解出哪里出了问题。如果您有任何帮助,我们将不胜感激。谢谢您缺少一个“/”。该行:

NSString *imageName = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"%d", myUniqueID]];
应该是:

NSString *imageName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", myUniqueID]];
NSString *imagePath = [imageName stringByAppendingPathExtension:@"png"];
这句话说:

NSString *imagePath = [imageName stringByAppendingPathComponent:@".png"];
应该是:

NSString *imageName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", myUniqueID]];
NSString *imagePath = [imageName stringByAppendingPathExtension:@"png"];

更新:

而且,不应该:

NSData *webData = UIImagePNGRepresentation(editedImage);
下面是什么

NSData *webData = UIImagePNGRepresentation(imageToUse);

谢谢你的回复。做出了改变。。但还是什么都没有。非常困惑。我不知道为什么。。。但更改NSData*webData=UIImagePngResentation(编辑图像);到NSData*webData=[NSData dataWithData:UIImagePNGRepresentation(editedImage)];似乎有工作我不知道为什么它第一次不工作,但我敢打赌,如果您将
NSData
行更改回,它仍然可以工作…我们修复了路径问题。现在,您需要检查webData的结果(即查看它有多大,等等)。顺便说一句,我注意到您一直在努力找出
imageToUse
,但之后您只需使用
editeImage
。看看我更新的答案。哈哈。那太尴尬了。现在工作完美无瑕。非常感谢@Rob!!