views:

33

answers:

2

Hi, 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 :)

+2  A: 

To me it looks like you don't have an UIImage there at that point in time but something else. This can be because your cocktail.image is somehow already released.

However, I never used Core Data so far, so I might be totally off :-) (BTW, are you sure that what you have there is actually an UIImage?).

Stelian Iancu
it seems to be a NSConcreteData Object, but I don't know why, the code seems right :(
patrick
Well, NSConcreteData is, for all purposes, NSData. So it might be that you're missing the step where the UIImage is created from that NSData that you have in the database. I would try to debug there, where the UIImage is supposed to be created from the NSData.
Stelian Iancu
It should be in this method: reverseTransformedValue, right? (again, never used Core Data). If yes, then you could create an intermediary UIImage there and eventually save it to the disk (or send there a message to this intermediary UIImage to be sure it's successfully created).
Stelian Iancu
seems that the ImageToDataTransformer isn't used :( that's strange... however I used this code: UIImage *myImg = [UIImage imageWithData:(NSData *)cocktail.image]; and it works, thanks for the tip :)
patrick
+1  A: 

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.

fluchtpunkt
mhm, I save it with this code [cocktail setImage:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.objectgraph.com/images/og_logo.png"]]]; I'll try using an UIImage
patrick
You see, you are not saving an UIImage.
fluchtpunkt
works! thank you very much :D
patrick