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

标签 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

导入“ViewController.h”

导入“AppDelegate.h”

@interface ViewController()

@结束

@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 上设置图像。

有关此的任何帮助都会很棒。我也对将视频存储在核心数据中感到困惑。请建议正确的处理方法。

最佳答案

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

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

关于ios - 核心数据和可转换检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32278862/

相关文章:

php - 连接失败 : 0 push notification

objective-c - 具有 Swift 结构的 C API - 作为 inout 参数的不可变值

ios - Objective-C - 'sendSynchronousRequest:returningResponse:error:' 已弃用 : first deprecated in iOS 9. 0

iphone - 如何保护 .ipa?

core-data - 使用 CoreData 生成器在 DerivedData 文件夹中复制文件

ios - 核心数据迁移: How should i change the data type from bool to int in next version

javascript - 如何在 React Native 中添加多个组件?

iphone - 将 'level pack' 添加到现有的 iPhone 应用程序

core-data - 在 CoreData 中保存 Swift CLLocation

iphone - iPad 2x 模式和 iPhone4 Retina 显示屏(针对开发者)之间有什么区别吗?