从URL上拉出的图像没有出现在UIImageView上

时间:2014-12-17 06:22:32

标签: ios objective-c image url uiimageview

我编写了一个应用程序,它从RSS源获取图像的URL并将其显示在UIImageView中,我知道我拉的图像是有效的,因为图像的URL直接显示在我的标签上网址和我输入它,它会转到图像。这是一个jpg图像。令人讨厌的是,即使我手动输入图像的网址,任何图像都不起作用。

这是我的代码,图片查看代码位于底部。

- (void)viewDidLoad {
    [super viewDidLoad];




    NSURL *URL = [NSURL URLWithString:@"http://rss.cnn.com/rss/edition_world.rss"];
    NSData *data = [NSData dataWithContentsOfURL:URL];

    // Assuming data is in UTF8.
    NSString *string = [NSString stringWithUTF8String:[data bytes]];
    titleLabelLatestNews.text = string;


    NSString *webString = titleLabelLatestNews.text;
    NSScanner *stringScanner = [NSScanner scannerWithString:webString];
    NSString *content = [[NSString alloc] init];
    while ([stringScanner isAtEnd] == NO) {
        [stringScanner scanUpToString:@"url=\"" intoString:Nil];
        [stringScanner scanUpToString:@"/>" intoString:&content];

        NSString *filteredImage = [content stringByReplacingOccurrencesOfString:@"url=\"" withString:@""];
        NSString *filteredImage2 = [filteredImage stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        titleLabelLatestNews.text = filteredImage2;

        NSURL * imageURL = [NSURL URLWithString:filteredImage2];
        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage * image = [UIImage imageWithData:imageData];
    [imageView setImage:image];



    }

我用于imageView的代码 -

NSURL * imageURL = [NSURL URLWithString:filteredImage2];
            NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
            UIImage * image = [UIImage imageWithData:imageData];
        [imageView setImage:image];

是因为JPG吗? 是因为我的ImageView吗? 一切都联系起来,一切都很好,除了这个,我已经看到了关于如何从网址获取图像的所有帖子,我尝试了一切,但似乎没有任何工作,图像视图保持空白和白色。

摘要: 我从网站上拉了一个图像链接,链接是有效的,因为我测试了它,我尝试下载和放入图像视图的任何其他图像也没有工作,图像视图是空白的,根本没有显示任何内容。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

好的,你没有看到图像,因为imageData是零。 imageData是nil,因为imageURL也是nil。

在创建NSURL之前,您需要转义url字符串。

而不是:

NSURL * imageURL = [NSURL URLWithString:filteredImage2];

执行:

NSURL * imageURL = [NSURL URLWithString:[filteredImage2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:0)

要使用NSURLSession下载文件,请执行以下操作:

 NSURLSession *downloadFileSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDownloadTask *downloadTask =[downloadFileSession downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

// handle the response. 'location' is the file you downloaded

}];

[downloadTask resume];

但同样,NSData应该完成这项工作。在开始下载之前,请重新检查图像的URL。您正在修改网址,因此请确保其有效。

答案 2 :(得分:0)

虽然这是一个基本代码,但我认为我解决了它

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[imgView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:imgView];

NSURL *URL = [NSURL URLWithString:@"http://rss.cnn.com/rss/edition_world.rss"];
NSData *data = [NSData dataWithContentsOfURL:URL];

// Assuming data is in UTF8.
NSString *string = [NSString stringWithUTF8String:[data bytes]];


NSScanner *stringScanner = [NSScanner scannerWithString:string];
NSString *content = [[NSString alloc] init];
while ([stringScanner isAtEnd] == NO)
{
    [stringScanner scanUpToString:@"url=\"" intoString:Nil];
    [stringScanner scanUpToString:@"/>" intoString:&content];

    NSString *filteredImage = [content stringByReplacingOccurrencesOfString:@"url=\"" withString:@""];
    NSString *filteredImage2 = [filteredImage stringByReplacingOccurrencesOfString:@"\"" withString:@""];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1
        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[filteredImage2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        dispatch_async(dispatch_get_main_queue(), ^{ // 2

            if(imgData)
            {
                UIImage *img = [UIImage imageWithData:imgData];
                if(img)
                {
                    [imgView setImage:img];
                }
                else
                {
                    NSLog(@" image url was %@ ",filteredImage2);
                }

            }
            else
            {
                 NSLog(@" image url was %@ ",filteredImage2);
            }

        });
    });
}