【问题标题】:Saving and loading UIImage in an iPhone app在 iPhone 应用程序中保存和加载 UIImage
【发布时间】:2012-07-22 23:33:51
【问题描述】:

我在弄清楚我的应用出了什么问题时头疼。我正在尝试保存和加载 UIImages,简单吧?好像不是这样。

我有一个具有这些方法的类来保存、加载和删除具有唯一字符串的图像。就像我喜欢的那样,又好又简单。这是课程:

imageSavedLoad.h:

#import <Foundation/Foundation.h>

@interface imageSaveLoad : NSObject

- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName;
- (void)removeImage:(NSString*)fileName;
- (UIImage*)loadImage:(NSString*)imageName;

@end

imageSaveLoad.m:

#import "imageSaveLoad.h"


@implementation imageSaveLoad

//saving an image
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName
{
    NSData *imageData = UIImagePNGRepresentation(image);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
    NSLog(@"image saved");
}

//removing an image
- (void)removeImage:(NSString*)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed");
}

//loading an image
- (UIImage*)loadImage:(NSString*)imageName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    return [UIImage imageWithContentsOfFile:fullPath];
}

@end

我要保存、加载或删除图像的地方我导入类:#import "imageSaveLoad.h"

然后我创建它的一个实例并调用所需的方法(这里我保存):

imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
[imgSL saveImage:photoImgView.image forKey:@"yourPhoto"];

然后在我要检索图像的另一个视图/类中:

imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
yourImgView.image = [imgSL loadImage:@"yourPhoto"];

但是yourImgView 什么都不显示,空白。

那么我哪里错了?我检查了所有IBOutlet 连接并且所有代码都被调用了。我使用的方法有问题吗?

【问题讨论】:

  • 请为我们记录以下内容:您保存 UIImage 的完整路径,您要从其中加载 UIImage 的加载路径,请务必检查保存后:图片文件是否在Documents文件夹中?
  • 您应该提供一个错误并查看 [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; 的返回码; - 你只是假设成功。假设 Documents 目录现在用于 iCloud,您可能需要为此使用 caches 目录。
  • 这些方法可以是类方法。只是说'

标签: iphone objective-c load uiimage save


【解决方案1】:

保存图像时,尝试替换此行:

 [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

 [imageData writeToFile:fullpath atomically:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2016-11-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    相关资源
    最近更新 更多