【问题标题】:Saving image persistently in within App - iOS将图像持久保存在 App 中 - iOS
【发布时间】:2012-10-07 18:59:41
【问题描述】:

尝试使用照片选择器选择图像并将该图像内部保存在应用程序文件夹中。

- (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 中没有返回值所以我不知道出了什么问题。任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: iphone ios image ios6 data-persistence


    【解决方案1】:

    您缺少一个“/”。行:

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

    应该是:

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

    那一行写着:

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

    应该是:

    NSString *imagePath = [imageName stringByAppendingPathExtension:@"png"];
    

    更新:

    而且,不应该:

    NSData *webData = UIImagePNGRepresentation(editedImage);
    

    是以下吗?

    NSData *webData = UIImagePNGRepresentation(imageToUse);
    

    【讨论】:

    • 感谢您的回复。进行了更改..但仍然没有。最困惑。
    • 我不知道为什么...但是改变 NSData *webData = UIImagePNGRepresentation(editedImage);到 NSData *webData = [NSData dataWithData :UIImagePNGRepresentation(editedImage)];似乎奏效了
    • 我不知道为什么它第一次没有工作,但我敢打赌,如果你把 NSData 改回来,它仍然可以工作......
    • 我们修复了路径问题。现在您需要检查 webData 的结果(即查看它有多大等)。顺便说一句,我注意到你努力找出imageToUse,但你只使用editedImage。请参阅我的更新答案。
    • 哈哈。这很尴尬。现在完美无缺。非常感谢@Rob !!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多