2012-04-25 9 views
6

私はコアデータに関連する初心者です。私は、画像を保存するときに文字列データを保存することができますが、画像を保存しようとするとアプリケーションがクラッシュすることがあります。節約のために :retrivalについてはコアデータに画像を保存する

DataEvent *event = (DataEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"DataEvent" 
                  inManagedObjectContext:managedObjectContext]; 
NSURL *url2 = [NSURL URLWithString:@"xxxxxxxxxxxxxxx SOME URL xxxxxxxxxxxx"]; 

NSData *data = [[NSData alloc] initWithContentsOfURL:url2]; 
imageSave=[[UIImage alloc]initWithData:data]; 
NSData * imageData = UIImageJPEGRepresentation(imageSave, 100.0);  
[event setValue:self.imageSave forKey:@"pictureData"]; 

DataEvent *event = (DataEvent *)[eventsArray objectAtIndex:indexPath.row]; 
UIImage *image = [UIImage imageWithData:[event valueForKey:@"pictureData"]]; 
UIImageView *imageViewMainBackGround = [[UIImageView alloc] 
CGRect rect3=CGRectMake(0,2,100.0,100.0); 
imageViewMainBackGround.frame = rect3; 
[cell.contentView addSubview:imageViewMainBackGround]; 
[imageViewMainBackGround release]; 
+0

モデルファイルの "pictureData" のデータ型とは何ですか? –

+0

そのバイナリデータ型 –

+0

"ストアを開くために使用されたモデルはストアを作成するために使用されたモデルと互換性がありません"このエラーはどこで得られますか?この行の代わりに –

答えて

0

あなたは(あなたのケース画像データで)NSDataオブジェクトへの管理対象オブジェクトのpictureData値を設定する必要があり、ないimageSaveオブジェクトは、UIImageです。

1

ことが必要ではない場合、解像度を変更しないでください。..

NSData * imageData = UIImageJPEGRepresentation(imageSave, 0.0); 

//と、次の行

[event setValue:imageData forKey:@"pictureData"]; 

See the store and Retrieve image from core data tutorial with sample code.

を変更希望、これは

..あなたを助けます
+0

こんにちは、こんにちは、私はエラーが発生した後:理由= "ストアを開くために使用されたモデルは、ストアを作成するために使用されたモデルと互換性がありません"; –

+0

ここで貯蓄や回収..? – Nit

+0

in画像データを保存する –

2

error: reason = "The model used to open the store is incompatible with the one used to create the store"

ソリューション:

ビルドプロジェクトをシミュレータから削除し、[製品]タブから削除して、プロジェクトを実行します。

NSData *imageData = UIImagePNGRepresentation(myUIImage); 

[newManagedObject setValue:imageData forKey:@"imageKey"]; 

そして をイメージRetriveするには:保存するには

9

NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"imageKey"]]; 
[[newCustomer yourImageView] setImage:image]; 

変更された形式