How to serialize UIImage to JSON?

前端 未结 3 2125
北荒
北荒 2021-02-06 07:45

I am using

imageData = UIImagePNGRepresentation(imgvw.image);

and while posting

[dic setObject:imagedata forKey:@\"image\"]; 
         


        
3条回答
  •  爱一瞬间的悲伤
    2021-02-06 08:04

    you can Convert Your image in ti NSData like:-

    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);
    

    and you can store it in to CoreData may be like this way is usefull for you:-

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

    You can load like :-

     NSManagedObject *selectedObject = [[self yourFetchCOntroller] objectAtIndexPath:indexPath];
          UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"image"]];
    // and set this image in to your image View  
        yourimageView.image=image;
    

提交回复
热议问题