核心数据和可转换的图像检索

时间:2015-08-28 19:54:24

标签: ios objective-c core-data nsmanagedobject transformable

我正在尝试使用Transformable将图像存储在核心数据中。我看到它的方式,我已经能够将图像保存在核心数据中,但我遇到的问题是检索它。这是我到目前为止所做的事情

// NSManagedObject的自定义子类

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "ImageConverter.h"

@interface Image : NSManagedObject

@property (nonatomic, retain) id storedImage;


@end



#import "Image.h"


@implementation Image

@dynamic storedImage;

@end

//NSTransformable Subclass
#import <Foundation/Foundation.h>

@interface ImageConverter : NSValueTransformer

@end


#import "ImageConverter.h"

@implementation ImageConverter

+ (Class)transformedValueClass
{
    return [NSData class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (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

// ViewController类,我将图像保存到核心数据中,然后在按钮上单击

来检索图像
#import <UIKit/UIKit.h>
#import "Image.h"
@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

import&#34; ViewController.h&#34;

import&#34; AppDelegate.h&#34;

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSManagedObjectContext *context =  [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    Image *event = [NSEntityDescription insertNewObjectForEntityForName: @"Image" inManagedObjectContext:context];
   // self.imageView.image = [UIImage imageNamed:@"Bus.png"];
    event.storedImage = [UIImage imageNamed:@"Bus.png"];
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    //    NSEntityDescription *entity= [NSEntityDescription entityForName:@“Event" inManagedObjectContext : context];
    //
    //

    NSManagedObjectContext *context =   [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Image" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error ;
    NSArray *fetchResults =
    [context executeFetchRequest : fetchRequest error : &error];
    for (Image *objects in fetchResults){
        if(objects.storedImage != nil) {
            NSLog(@"%@",objects.storedImage);
        }
        self.imageView.image = objects.storedImage
    }


}

@end

我希望将检索到的图像视为

        self.imageView.image = objects.storedImage.

但到目前为止我还没能找到它。 当我NSLog ImageCoreData [3253:108752] UIImage:0x7fbec1560f70,{0,0}被检索但在UIImageView上没有设置图像。

对此有任何帮助都会很棒。我也很担心将视频存储在核心数据中。请建议正确的方法来接近它。

1 个答案:

答案 0 :(得分:2)

这是因为您正在使用imageNamed:来创建图片。由于Core Data(或可能是键控存档)中的明显错误,以这种方式创建的图像无法保存到Core Data可转换属性中。没有错误发生,但您以后无法检索图像。 (rdar://20717042适用于任何可能感兴趣的人。)

如果您创建图像任何其他方式,那么它应该有效。例如,如果您使用imageWithContentsOfFile:,那么您就可以了。但是你不能在这里使用imageNamed: