[英]Core Data and Transformable To Retrieve Image
I am trying to store image in core data using Transformable. 我正在尝试使用Transformable将图像存储在核心数据中。 The way I see it , I have been able to save the image in core data but the problem I have is retrieving it.
以我的方式看,我已经能够将图像保存在核心数据中,但是我遇到的问题是对其进行检索。 Here's what I have done so far
到目前为止,这是我所做的
//Custom subclass of NSManagedObject // 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 class where I save the image into core data and retrieve the image back on button click // ViewController类,将图像保存到核心数据中,然后单击按钮重新取回图像
#import <UIKit/UIKit.h>
#import "Image.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@interface ViewController () @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
I expect to see the retrieved image as 我希望看到检索到的图像为
self.imageView.image = objects.storedImage.
But so far I have not been able to retrieve it. 但是到目前为止,我还无法检索它。 When I NSLog ImageCoreData[3253:108752] UIImage: 0x7fbec1560f70, {0, 0} is retrieved but no image is set on UIImageView.
当我NSLog ImageCoreData [3253:108752] UIImage:0x7fbec1560f70时,检索到{0,0},但未在UIImageView上设置任何图像。
Any help regarding this will be great. 关于此的任何帮助将是巨大的。 I am also confused about storing the video in core data.
我也对将视频存储在核心数据中感到困惑。 Please suggest the right way to approach it.
请提出正确的处理方法。
It's because you're using imageNamed:
to create the image. 这是因为您正在使用
imageNamed:
创建图像。 Due to an apparent bug in Core Data (or maybe keyed archives), images created this way can't be saved to Core Data transformable attributes. 由于核心数据(或可能是带密钥的存档)中存在明显的错误,因此以这种方式创建的图像无法保存到核心数据可转换属性中。 No errors occur, but you can't retrieve the image later on.
没有错误发生,但是以后无法检索图像。 (
rdar://20717042
for anyone who might be interested). (
rdar://20717042
对于可能感兴趣的任何人)。
If you create the image any other way then it should work. 如果您以其他方式创建图像,则它应该可以工作。 For example if you use
imageWithContentsOfFile:
, you'll be fine. 例如,如果您使用
imageWithContentsOfFile:
那就可以了。 But you can't use imageNamed:
here. 但是您不能在此处使用
imageNamed:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.