我正在从URL下载图像并保存。当我尝试使用ImageView在UI中显示相同的图像时,我无法做到这一点。因此,请在以下代码中提出我需要做的更改。

谢谢。

{

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

}

最佳答案

UIImage *theUIimage = [UIImage imageNamed:sss];
image = [[UIImageView alloc] initWithImage:theUIimage];
[imageView1 addSubview:image];

10-06 13:24