0

I have used the following to save base64 image data.

NSData *data=[NSData dataFromBase64String:base64];
UIImage *image= [UIImage imageWithData:data];

It is always saved as a png type.

How can I set the type of the image other than png (jpeg etc.) ?

madLokesh
  • 1,860
  • 23
  • 49
Gobi M
  • 3,243
  • 5
  • 32
  • 47
  • 3
    See the top answer to http://stackoverflow.com/questions/1150911/save-image-to-sandbox-from-uiimage-in-iphone – BergQuester Aug 07 '13 at 04:04

2 Answers2

2

After getting the UIImage you will have to convert it back into data with different format then you can get png and jpg or jpeg UIImage.

NSData *data1 = UIImageJPEGRepresentation(image, 1.0); // 1.0 is for original you can compress it by passing the float b/w 0-1
NSData *data2 = UIImagePNGRepresentation(image);

UIImage *jpgImage = [UIImage imageWithData:data1];
UIImage *pngImage = [UIImage imageWithData:data2];

Or you can also write data with image extensions in documents directory.

[data writeToFile:[directoryPath stringByAppendingPathComponent:@"image.jpeg"] atomically:YES];
// It will save this image as jpeg. But would not work for all formats.

Edit: See this link How to convert .jpg image to .bmp format using Objective C?.

Community
  • 1
  • 1
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • only these two types are available in ios? – Gobi M Aug 07 '13 at 04:46
  • What formats do you want ? – TheTiger Aug 07 '13 at 04:48
  • 1
    You can't convert it directly ... See edit part. – TheTiger Aug 07 '13 at 05:14
  • Or you can also googling for that ... https://www.google.co.in/search?biw=1019&bih=625&noj=1&sclient=psy-ab&q=convert++image+to+bmp+%2B+ios&oq=convert++image+to+bmp+%2B+ios&gs_l=serp.12...168784.168784.1.169641.1.1.0.0.0.0.213.213.2-1.1.0....0...1c.1.24.serp..1.0.0.id5I-BUkqi4 – TheTiger Aug 07 '13 at 05:16
0

Have you tried UIImageJPEGRepresentation?

Chris Culter
  • 4,470
  • 2
  • 15
  • 30