我正在處理的應用程序允許用戶從相機膠捲中選取圖像,並將其顯示在UIImageView中。現在,我在嘗試保存數據時遇到了一些問題。我知道我必須將圖像轉換爲PNG並保存數據,但我遇到了一些問題。一般來說,我對文件保存和Objective-C非常陌生。下面是我使用的代碼:iOS:將圖像轉換爲PNG以允許保存
- (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];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image.image)];
[array addObject:imageData];
[array writeToFile:[self dataFilePath] atomically:YES];
}
- (void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
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];
}
程序崩潰時只需輕敲標籤,這發生在調試狀態「‘NSInvalidArgumentException’的,原因是:「 - [__ NSCFData _isResizable]:無法識別的選擇發送到實例0x16cf50'「。任何幫助非常感謝,謝謝!
太棒了,非常感謝! – John 2012-03-28 16:59:39
沒問題。很高興幫助 – David 2012-03-28 17:00:38
雖然圖像仍然不能保存,你知道我應該如何編輯代碼來保存它嗎?再次感謝! – John 2012-03-28 17:34:11