簡體   English   中英

如何在iPhone中顯示從URL下載的圖像?

[英]How to display a image downloaded from URL in iPhone?

我正在從網址下載圖像並保存。 當我嘗試使用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];

你的imageView是如何初始化的? 確保還指定imageView.frame

例如:

CGRect frame = CGRectMake(xPositionInView,yPositionInView,
                          image.size.width,image.size.height); 
imageView.frame = frame;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM