Convert UIImage to NSData and save with core data

前端 未结 8 771
不知归路
不知归路 2021-01-30 18:43

I have a UIImageView whose image gets set via UIImagePicker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPi         


        
相关标签:
8条回答
  • 2021-01-30 19:12

    Here is what I have done and its working for storing to nsdata.

    [productTypeSub setValue:[[NSData alloc] initWithContentsOfURL:[NSURL  URLWithString:url]] forKey:@"imgSmall"];
    

    and loading to image with this code

    ImgProductType.image= [[UIImage alloc] initWithData:productTypeSub.imgSmall];
    
    0 讨论(0)
  • 2021-01-30 19:17

    You can convert a UIImage to NSData like this:

    If PNG image

    UIImage *image = [UIImage imageNamed:@"imageName.png"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    

    If JPG image

    UIImage *image = [UIImage imageNamed:@"imageName.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    

    You can store it in CoreData like so (this is one possible useful solution):

    [newManagedObject setValue:imageData forKey:@"image"];
    

    You can load the data from CoreData like this:

    NSManagedObject *selectedObject = [[self yourFetchCOntroller] objectAtIndexPath:indexPath];
    UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"image"]];
    
    // Set the image to your image view  
    yourimageView.image = image;
    
    0 讨论(0)
提交回复
热议问题