Convert UIImage to NSData and save with core data

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

I have a UIImageView whose image gets set via UIImagePicker

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


        
相关标签:
8条回答
  • 2021-01-30 18:50

    On Mac OS you can let Core Data handle this for you by ticking the Transformable box on the attribute in the model, not selecting a specific transformer, and assigning the UIImage directly to the property.

    I'm not sure if it works on iOS but it might be worth a try.

    0 讨论(0)
  • 2021-01-30 18:55

    This code save your image data into filedata and you can use it anywhere

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image1 editingInfo:(NSDictionary *)editingInfo
    {
    
        {
    
            CGSize destinationSize = CGSizeMake(170,170);
    
            UIGraphicsBeginImageContext(destinationSize);
    
           [image1 drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
    
           UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
           UIGraphicsEndImageContext();
    
           NsData *filedata  = UIImagePNGRepresentation(newImage);
    
           [picker dismissModalViewControllerAnimated:YES];
       }
    }
    
    0 讨论(0)
  • 2021-01-30 18:57

    To convert an image to NSData try below code...

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

    let me know it is working or not..!!!

    Happy Coding!!!!

    0 讨论(0)
  • 2021-01-30 18:58

    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.

    0 讨论(0)
  • 2021-01-30 19:01

    For swift its just:

    UIImage to NSData

    imageData = UIImagePNGRepresentation(image)
    
    0 讨论(0)
  • 2021-01-30 19:07

    Try This Code to save image data

    to save:

    NSManagedObjectContext *context = [self managedObjectContext];
    
    newsObj = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
    
    NSData *imageData = UIImagePNGRepresentation(self.gImage.image);
    
    [newsObj setValue:imageData forKey:@"imgPng"];
    
    NSError *error;
    
    @try{
    
        if (managedObjectContext != nil) {
    
            if (![managedObjectContext save:&error]) {
    
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    
    
            } 
        }
    
    }@catch (NSException *exception) {
    
        NSLog(@"inside exception");
    }
    

    To Retrive Try this:

    NSManagedObjectContext *context = [self managedObjectContext];
    
        NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    
        NSEntityDescription *entity1 = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
    
        [fetchRequest setEntity:entity1];
    
        NSError *error;
    
        NSArray * array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
        if (array == nil) {
    
            NSLog(@"Testing: No results found");
    
        }else {
    
            NSLog(@"Testing: %d Results found.", [array count]);
        }
    
        NSData * dataBytes = [[array objectAtIndex:0] imgPng];;
    
        image = [UIImage imageWithData:dataBytes];
    
        [fetchRequest release]; 
    
    
    }
    
    @catch (NSException *exception) {
    
        NSLog(@"inside exception");
    }
    
    0 讨论(0)
提交回复
热议问题