2012-03-28 79 views
1

我正在處理的應用程序允許用戶從相機膠捲中選取圖像,並將其顯示在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'「。任何幫助非常感謝,謝謝!

回答

1

好的,第一件事情是第一件事,第二件事是你進入了執行這些操作的「標籤」的錯誤,是的?所以錯誤應該在你的viewDidLoad方法中。其次,你的錯誤要求選擇器錯誤,所以找到你使用@selector的地方。

這是你的問題,導致錯誤:

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(applicationDidEnterBackground:) 
              name:UIApplicationDidEnterBackgroundNotification 
              object:app]; 

注意,這個方法applicationDidEnterBackground有一個參數(NSNotification *):你的應用程序在發送

- (void)applicationDidEnterBackground:(NSNotification *)notification { 

,然而,這是一個UIApplication:

UIApplication *app = [UIApplication sharedApplication] 

這是我唯一能看到的窩窩會給你這個錯誤。只需將其更改爲這和錯誤應該消失:

- (void)applicationDidEnterBackground:(UIApplication*)application { 

BEGIN編輯:

至於保存圖像,我不知道究竟你是如何存儲數據,但這是我該怎麼做的。

  1. 使用保留的屬性來存儲圖像的數據。

ViewController.h

@interface ViewController 

//Add this line 
@property(retain) NSData* imageData; 

@end 

ViewController.m

//Remember to include this line somewhere in this file: 
@synthesize imageData; 

當你要保存文件:

- (void)applicationDidEnterBackground:(UIApplication*)application { 
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(image.image)]; 

[self.imageData writeToFile:[self dataFilePath] atomically:YES]; 

}

這種方式不會丟失數據,也不需要使用數組。只要確保加入這行,以及在viewDidUnload方法:

self.imageData = nil; 

編輯完

希望幫助你。

+0

太棒了,非常感謝! – John 2012-03-28 16:59:39

+1

沒問題。很高興幫助 – David 2012-03-28 17:00:38

+0

雖然圖像仍然不能保存,你知道我應該如何編輯代碼來保存它嗎?再次感謝! – John 2012-03-28 17:34:11

0

提供yourUIImage有效且包含的數據,你可以叫

[imageData writeToFile:(NSString*) atomically(BOOL)] 

無需創建一個數組和寫入。