Ajay Venkat
Ajay Venkat

Reputation: 9

Images pulled from url not appearing on UIImageView

I wrote an app which goes and gets the url of an image from a RSS feed and displays it in a UIImageView and I know the image that I am pulling is valid because the url of the image goes straight to my label which shows the url and i typed it in and it goes to a image. It is a jpg image. What is annoying is that any image doesn't work even if I manually enter an url for an image.

This is my code , the image view code is down the bottom.

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



    }

The code I am using for the imageView -

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

Is it because of JPG? Is it because of my ImageView? Everything is linked up and everything is working fine except this I have seen all posts on how to get image from url and I tried everything but nothing seems to work the image view stays blank and white.

Summary : I am pulling a image link from a website and the link is valid because I tested it and any other image i try downloading and putting in the image view also doesn't work the image view is blank and shows nothing at all.

Thank you for your help!

Upvotes: 0

Views: 1447

Answers (3)

Debanjan Chakraborty
Debanjan Chakraborty

Reputation: 191

Though this is a rudimentary code, I think I solved it

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);
            }

        });
    });
}

Upvotes: 0

Avi Tsadok
Avi Tsadok

Reputation: 1843

Ok, you don't see an image, because imageData is nil. imageData is nil, because imageURL is also nil.

You need to escape the url string before you create NSURL.

Instead of :

NSURL * imageURL = [NSURL URLWithString:filteredImage2];

Do:

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

Upvotes: 1

Avi Tsadok
Avi Tsadok

Reputation: 1843

To download a file using NSURLSession, do something like that:

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

But again, NSData should do the job. Re-check the url of the image, just before you start download it. You are modifying the url, so make sure it is valid.

Upvotes: 0

Related Questions