- (IBAction)saveImage:(id)sender{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
images.image = image;
[image setValue:tmpphoto forKey:@"img"];
CGSize size = tmpphoto.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = 90.0/size.width;
} else {
ratio = 90.0/size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
[tmpphoto drawInRect:rect];
images.imaget = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
我的實體名稱爲「圖像」與屬性IMG(變形),imgcode(串),我嘗試這個圖像保存到我的設備與核心data.But我有這個錯誤,是什麼可以是問題嗎?預先感謝您...保存圖像設備與coredata
錯誤:「NSInvalidArgumentException」的,理由是:圖片「
我不能評論你的問題的核心數據方面,但有兩點意見:1.你可以使用['UIImagePNGRepresentation'](http://developer.apple.com/library/ios/documentation/uikit/reference /UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation)或'UIImageJPEGRepresentation'來檢索圖像,而不是圖形上下文方法; 2.圖像(除非非常小,小於100kb)不適合存儲在CoreData中。 – Rob
並且當我評論這兩行時 AppDelegate * appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext * context = [appDelegate managedObjectContext]; 而不是這些行 NSManagedObject * image = [NSEntityDescription insertNewObjectForEntityForName:@「Image」inManagedObjectContext:self.managedObjectContext];我做這個我的錯誤正在改變爲entityForName:nil不是合法的NSManagedObjectContext參數搜索實體名稱'圖像'??? – santa