0
我正在從網站下載圖片並保存。當我嘗試在使用ImageView的UI中顯示相同的圖像時,我無法做到這一點。所以請在下面的代碼中建議我需要做些什麼改變。如何在iPhone中顯示從URL下載的圖像?
謝謝。
{
NSLog(@"Downloading...");
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.objectgraph.com/images/og_logo.png"]]];
NSLog(@"%f,%f",image.size.width,image.size.height);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",docDir);
NSLog(@"saving png::::%@", docDir);
NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];
NSString *sss = [docDir stringByAppendingString:@"/test.png"];
imageView1.image = [UIImage imageNamed:sss];
NSLog(@"------%@",sss);
[image release];
}
小評此'的NSData * DATA1 = [NSData的dataWithData:UIImagePNGRepresentation(圖像)];'相同'NSData的* DATA1 = UIImagePNGRepresentation(圖像);'。但是現在你並沒有創建一個能夠節省內存的對象。 – rckoenes