Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UIImage to NSData and save with core data

I have a UIImageView whose image gets set via UIImagePicker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.gImage.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

I "attempt" to convert this image to NSData and save it with core data:

NSData *imageData = UIImagePNGRepresentation(self.gImage.image);
NSString *savedData = [[NSString alloc]initWithData:imageData encoding:NSUTF8StringEncoding];

//am is a pointer to my entities class. imageData is just a NSString attribute
am.imageData = savedData;

NSError *error;
if (![self.managedObjectContext save:&error]) {
    //Handle Error
} else {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Then I try to load the image in a separate file:

self.cell.gImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.myEntity.imageData]]];

I cannot see why this is not working. Any help is greatly appreciated!

like image 644
Bad_APPZ Avatar asked Jan 03 '13 05:01

Bad_APPZ


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do you convert CIImage to UIImage?

This is most compatibile way of doing it: UIImage* u =[UIImage imageNamed:@"image. png"]; CIImage* ciimage = [[CIImage alloc] initWithCGImage:u. CGImage];


3 Answers

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;
like image 129
Nitin Gohel Avatar answered Oct 22 '22 01:10

Nitin Gohel


For swift its just:

UIImage to NSData

imageData = UIImagePNGRepresentation(image)
like image 29
RiceAndBytes Avatar answered Oct 22 '22 00:10

RiceAndBytes


For storing in database:::

        NSInteger RandomIndex = arc4random() % 1000; 
        NSString *randomImageName =[NSString stringWithFormat:@"Image%i.png",RandomIndex]; 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:randomImageName];
        if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]) {
            [[NSFileManager defaultManager] removeItemAtPath:savedImagePath error:nil];
            NSLog(@"file removed from path");
        }
        NSLog(@"Saved Image Path : %@",savedImagePath);
         NSData* imageData = UIImagePNGRepresentation (image1 ); 
        [imageData writeToFile:savedImagePath atomically:YES];

         //am is a pointer to my entities class. imageData is just a NSString attribute
        am.imageData = randomImageName;

When you get that image from data base, you can write following code...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:randomImageName];
NSData *myData = [NSData dataWithContentsOfFile:savedImagePath];
        UIImage *selectedImage = [UIImage imageWithData:myData];

I think it is helpful to you.

like image 31
Prasad G Avatar answered Oct 22 '22 01:10

Prasad G