How to save .jpg image as .png or .jpeg in iphone
I am saving images in my doc dir..I dont have the problem with .png or .jpeg images.I am able to display them properly in iphone.But coming to .jpg,i am not able to display.Please help me in this.THANKS IN ADVANCE
+(void)DownloadImage:(NSString*)ImagePath{
if ([ImagePath isEqualToString:@""]) {
return;
}
UIImage *dimage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://1xyz/uploads/%@",ImagePath]]]];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0开发者_开发问答];
NSLog(@"pathIMAGEPATHS:::::::%@",ImagePath);
NSLog(@"path:::::::%@",docDir);
NSArray *patharr=[ImagePath componentsSeparatedByString:@"."];
NSString* Ext=[NSString stringWithFormat: @"%@",[patharr objectAtIndex:1]];
NSString *FilePath = [NSString stringWithFormat: [NSString stringWithFormat:@"%@/%@",docDir,ImagePath]];
NSLog(@"%@/%@",docDir,ImagePath);
if([Ext isEqualToString:@"png"])
{
NSLog(@"saving png");
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(dimage)];
[data1 writeToFile:FilePath atomically:YES];
}
else if([Ext isEqualToString:@"jpeg"])
{
NSLog(@"saving jpeg");
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(dimage, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:FilePath atomically:YES];
}
else
{
NSData *data3 = [NSData dataWithData:UIImageJPEGRepresentation(dimage, 1.0f)];
[data3 writeToFile:FilePath atomically:YES];
}
NSLog(@"saving image done");
[dimage release];
}
You can use UIImagePNGRepresentation
. First create an UIImage
with your original JPG and them use this class method to save it as PNG.
UIImage *image = [UIImage imageNamed:@"yourImage.jpg"]
NSData *imageData = UIImagePNGRepresentation(image);
Once you have the data you can save to disk by calling writeToURL:
.
精彩评论