2012-05-04 24 views
-1

我試圖保存和加載從我的應用程序中的相機膠捲拾取的圖像。我得到一個錯誤,當我按下要麼加載或保存按鈕試圖在應用程序錯誤中保存/加載圖像NSInvalidArgumentException

*終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:「 - [UIImageView的CGImage]:無法識別的選擇發送到實例0xe637640」

有什麼想法嗎?

AppDelagate的.m

- (NSURL *)applicationDocumentsDirectory 
{ 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory i  nDomains:NSUserDomainMask] lastObject]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

類Im的.M試圖保存/加載圖像

- (IBAction)save:(id)sender { 
UIImage *myImage = imageView; 
NSData *data = UIImagePNGRepresentation(myImage); 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *appDocsDirectory = [paths objectAtIndex:0]; 


} 

- (IBAction)load:(id)sender { 
UIImage *myImage = imageView; 
NSData *data = UIImagePNGRepresentation(myImage); 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *appDocsDirectory = [paths objectAtIndex:0]; 
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@.png", appDocsDirectory, @"myNewFile"]]; 
} 
+0

我最初的想法是,你沒有正確標記問題,你沒有包括一個堆棧跟蹤。 – trojanfoe

+0

真的很有幫助謝謝 – JSA986

+0

你似乎並不想幫助自己。你的標籤是錯誤的;他們都無關緊要(有人可以糾正他們,但爲什麼他們呢?)。如果您沒有正確標記問題,那麼正確的人可能會錯過它。在發生異常/崩潰的情況下,爲了找出問題所在,必須有堆棧跟蹤。所以如果你需要幫助,你需要提供更好的信息。 – trojanfoe

回答

1

此錯誤意味着消息CGImage被髮送到類型的UIImageView的對象,但的UIImageView不迴應它。問題是似乎是在

UIImage *myImage = imageView; 

你不寫正確實施的ImageView的,所以我不能確切地說,但我認爲ImageView的是UIImageView的,這是UIView的子類,且將其分配給UIImage的。嘗試

UIImage *myImage = [imageView image]; 

改爲。我認爲這應該工作))

順便說一句,爲什麼你需要第一和第二串代碼在-load?他們是不必要的:

- (IBAction)load:(id)sender { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *appDocsDirectory = [paths objectAtIndex:0]; 
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@.png", appDocsDirectory, @"myNewFile"]]; 
} 
+0

謝謝,這已經停止了拋出的異常,並且我已經擺脫了兩行冗餘代碼,感謝您指出並幫助您。現在我只需要讓圖像保存並加載! – JSA986

相關問題