Why is my transformable Core Data attribute not using my custom NSValueTransformer?

后端 未结 6 607
故里飘歌
故里飘歌 2020-11-27 17:08

I have a Core Data app with a fairly simple data model. I want to be able to store instances of NSImage in the persistent store as PNG Bitmap NSData objects, to save space.<

6条回答
  •  时光取名叫无心
    2020-11-27 18:05

    Check out the example code here.

    Does this do what you want?

    @implementation UIImageToDataTransformer
    
    
    + (BOOL)allowsReverseTransformation {
        return YES;
    }
    
    + (Class)transformedValueClass {
        return [NSData class];
    }
    
    
    - (id)transformedValue:(id)value {
        NSData *data = UIImagePNGRepresentation(value);
        return data;
    }
    
    
    - (id)reverseTransformedValue:(id)value {
        UIImage *uiImage = [[UIImage alloc] initWithData:value];
        return [uiImage autorelease];
    }
    

提交回复
热议问题