2011-02-09 108 views
0

我節省了我的文檔dir..I圖像不與巴紐問題或.jpeg images.I我能在iPhone中正確顯示它們。但是來到.JPG,我不能夠display.Please幫我this.THANKS IN ADVANCE如何.JPG圖像保存爲巴紐或.jpeg在iphone

+(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]; 

} 

回答

0

您可以使用UIImagePNGRepresentation。首先創建一個UIImage與您的原始JPG,他們使用這種類的方法保存爲PNG。

UIImage *image = [UIImage imageNamed:@"yourImage.jpg"] 
NSData *imageData = UIImagePNGRepresentation(image); 

一旦你有了數據,你可以通過調用writeToURL:保存到磁盤。

相關問題