ios - 从 coredata 接收图像

标签 ios objective-c

我用 camrea/gallery 手机拍了一张照片,然后将她存储在核心数据中。但我无法从核心数据接收回图像。

@property (strong) NSMutableArray *allPic;
@property (strong) NSManagedObject *Image;
@property(strong,nonatomic)NSData *dataImage;
@end

@implementation ViewController



- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Picture"];
   self.allPic = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self loadDefaultImage];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    // Creating a Circular Profile Image.
    self.img.layer.cornerRadius = self.img.frame.size.width / 2.0;
    self.img.clipsToBounds = YES;

    // Adding Border to image.
    self.img.layer.borderWidth = 6.0;
    self.img.layer.borderColor = [UIColor blackColor].CGColor;

    [self loadDefaultImage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePic:(id)sender {

    // ALERT SHEET.
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //CAMERA
    UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"צלם" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
    {
        // If device has no camera.
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertController *alertNoCamera = [UIAlertController alertControllerWithTitle:@"Error" message:@"Device has no camera" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];
            [alertNoCamera addAction:ok];
            [self presentViewController:alertNoCamera animated:YES completion:nil];
        }
        else// if  have a camera.
        {
           UIImagePickerController *picker = [[UIImagePickerController alloc] init];
           picker.delegate = self;
           picker.allowsEditing = YES;
           picker.sourceType = UIImagePickerControllerSourceTypeCamera;


           [self presentViewController:picker animated:YES completion:NULL];
        }
    }];
    // GALLERY
    UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"גלריה" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];
    }];

    [alert addAction:openCamrea];
    [alert addAction:openGallery];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
    {
        UIImage *sampleimage = info[UIImagePickerControllerOriginalImage];

        //sampleimage = [UIImage imageNamed:@"image"];

        self.dataImage = UIImageJPEGRepresentation(sampleimage, 0.0);

        [self.allPic setValue:_dataImage forKey:@"image"]; // obj refers to NSManagedObject
    }

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];
    }
-(void)loadDefaultImage
{

    UIImage *img2 = [UIImage imageWithData:_dataImage];
    self.img.image=img2;

}

我更新了问题 我再次编辑了问题,我写了所有的代码, 我拍照,保存,然后尝试将其取回。?

最佳答案

您可以试试这个从核心数据中获取图像。 从核心数据中获取您的数据,然后:

UIImage *img=[UIImage imageWithData:data];

您将数据保存为:

   [self.allPic setValue:_dataImage forKey:@"image"];

但是您已经将 `self.allPic' 声明为 NSArray。这将一直可用,直到该对象存在于内存中,这就是您无法取回它的原因。

您必须使用 NSMananaged 类型来保存核心数据值。

查看这篇文章:Tuts

关于ios - 从 coredata 接收图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33842626/

相关文章:

ios - 随机和数字图像不相同 iOS xCode

objective-c - 在 cocoa 中处理来自 MIDIReadProc 的传入 MIDI 信号的最佳策略

objective-c - NSComboBox : How to tell user has typed in information that is not in the pop up list and read it

ios - XCode 6.0.1 行为 : Huge Delays?

ios - 多个命令产生错误 xcode 10

iphone - 如何检查应用程序是否在后台运行

ios - 如何在 CCSprite 图像上设置闪烁?

ios - 旋转和导航后状态栏隐藏

objective-c - NSTextView,绑定(bind)和显示 NSString

ios - 将值从统一 3D 传递到 objective-c 文件