我有一個應用程序,允許用戶從他們的相機卷中選擇一張圖片,並將其顯示在UIImageView中。我通常使用這種方法來保存文本字段中的文本,並且我認爲它也適用於圖像,但我遇到了一些問題。沒有錯誤,但它不會保存圖像。iOS:在UIImageView中保存圖片
這是我使用的相關代碼:
.H:
#define kFilename9 @"PGdata.plist"
...
- (NSString *)dataFilePath;
.M:
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename9];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:image.image];
[array writeToFile:[self dataFilePath] atomically:YES];
}
- (void)viewDidLoad
{
...
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
image.image = [array objectAtIndex:0];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
[super viewDidLoad];
}
真棒非常感謝你。 – John 2012-03-27 19:49:25