我正在为我的应用程序出了什么问题而头疼。我正在尝试保存和加载 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
图像保存加载.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
连接并且所有代码都被调用了。我使用的方法有问题吗?