I’ve a problem when I try to access the image property of one Core Data Model:
#import <CoreData/CoreData.h>
@class IngredientWithQuantity;
@interface Cocktail : NSManagedObject
{
}
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * desc;
@property (nonatomic, retain) NSSet* ingredients;
@end
@interface Cocktail (CoreDataGeneratedAccessors)
- (void)addIngredientsObject:(IngredientWithQuantity *)value;
- (void)removeIngredientsObject:(IngredientWithQuantity *)value;
- (void)addIngredients:(NSSet *)value;
- (void)removeIngredients:(NSSet *)value;
@end
The image is set as a Transformable
#import <UIKit/UIKit.h>
@interface ImageToDataTransformer : NSValueTransformer {
}
@end
Implementation
#import "ImageToDataTransformer.h"
@implementation ImageToDataTransformer
+ (BOOL)allowsReverseTransformation {
return YES;
}
+ (Class)transformedValueClass {
return [NSData class];
}
- (id)transformedValue:(id)value {
if (value == nil) {
return nil;
}
// I pass in raw data when generating the image, save that directly to the database
if ([value isKindOfClass:[NSData class]]) {
return value;
}
return UIImagePNGRepresentation((UIImage *)value);
}
- (id)reverseTransformedValue:(id)value {
return [UIImage imageWithData:(NSData *)value];
}
@end
When I set the image it seems work fine but when I try to use it inside a view, with this code:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = cocktail.name; //works
self.descriptionView.text = cocktail.desc; //works
pictureView.image = cocktail.image; //crash
}
i get this error:
2010-10-12 17:22:25.409 PrimosBar[2399:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData scale]: unrecognized selector sent to instance 0xc027e00'
And I don’t know how to resolve 🙁
Can you help me?
Thanks 🙂
If I read NSConcreteData I think you are not saving an UIImage to your ManagedObject but an “pure” image, ie jpg, png etc.
Make sure you create an UIImage out of your pure data first. Maybe you want to post the code where you assign the image to the managed object.