0

I'm using the below to set my imageView with a url (located in path). Yes, the URL data is returned successfully, but for some reason, my imageView remains blank (just white?)

And yes, I've hooked up my imageView property... Any idea what's wrong? Should I be using a different block of code to accomplish this?

Viewcontroller.m

    NSMutableDictionary *viewParamsDogs = [NSMutableDictionary new];
    [viewParamsDogs setValue:@"mydogs" forKey:@"view_name"];
    [DIOSView viewGet:viewParamsDogs success:^(AFHTTPRequestOperation *operation, id responseObject) {

      self.dogData = [responseObject mutableCopy];

        [operation responseString];


        NSDictionary *dic = [responseObject valueForKey: @"field_pet_photo_path"];
                             NSArray *arr = [dic valueForKey: @"und"];
                             NSDictionary *dic2= [arr objectAtIndex : 0];
       NSString *path = [NSString stringWithFormat:@"%@", [dic2 valueForKey: @"safe_value"]];

      NSLog(@"This is path %@", path);



  if([path length]>0) {


      NSURL *url = [NSURL URLWithString:path];
      NSData *data = [NSData dataWithContentsOfURL:url];
      UIImage *image = [UIImage imageWithData:data];
      self.dogimageView.image = image;


   } else {

            NSString *ImageURL = @"http://url.ca/paw.png";
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
            self.dogimageView.image = [UIImage imageWithData:imageData];
      }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];

Path:

[5111:1673330] This is path (
    "http://url.com/default/files/stored/1460659054.jpg"
)

NSLog(@"This is path %@, %@", path, NSStringFromClass(path.class)); returns:

2016-04-14 13:18:39.590 [5225:1700657] This is path (
    "http://url.com/default/files/stored/1460659054.jpg"
), __NSCFString
  • URL is of server or is documents directory? – Ketan Parmar Apr 14 '16 at 19:03
  • @Lion server I believe? – Blair Warner Apr 14 '16 at 19:04
  • does imageData have any data in it? if not then check if your url has anything – Eugene Gordin Apr 14 '16 at 19:05
  • @EugeneZhenyaGordin Damn, lol imageData is null! HOW IS THAT POSSIBLE? When path is populated? haha – Blair Warner Apr 14 '16 at 19:09
  • check the answer if it can help. – Ketan Parmar Apr 14 '16 at 19:09
  • 1
    try in your browser if you can go to that url and see the image...cause I can't...I'm assuming you have to be signed in to that site to get the image, no? – Eugene Gordin Apr 14 '16 at 19:20
  • Use breakpoints, and see if the `imageData` has any value, if so then everything is good, except for your `dogimageView` property which most likely is an issue derived from misusage of Storyboard. Check your connections see if you renamed something or just simply delete and reconnect. – mfaani Apr 14 '16 at 19:22
  • 1
    @asma22 please read above, he already said that the data is nil – Eugene Gordin Apr 14 '16 at 19:23
  • @asma22 imageData has no value - I'm just not sure why, because path has the correct URL value...? – Blair Warner Apr 14 '16 at 19:23
  • @BlairWarner see my comment above – Eugene Gordin Apr 14 '16 at 19:24
  • @EugeneZhenyaGordin Lol...obv the url is a dummy (url.com?) ha ha. But yes, my user is logged in when the image is being grabbed anyway, and yes, I can see the image when I navigate to it in my browser. – Blair Warner Apr 14 '16 at 19:26
  • @BlairWarner ok, we now know that path is a valid NSString and should point to image. Next you should check if you grab NSData: `NSData *data = [NSData dataWithContentsOfURL:url]; NSLog(@"data: %@", data); UIImage *image = [UIImage imageWithData:data]; NSLog(@"image: %@", image); ` – Maciej Gad Apr 14 '16 at 20:35
  • @bazyl87 data is null, and image is null!! O_O Which makes sense, because url is also null... Wth is happening between path and url?! lol – Blair Warner Apr 14 '16 at 20:50
  • Could you check if `url` is `nil`? – Maciej Gad Apr 14 '16 at 20:56
  • @bazyl87 Yes, url is nil/null! – Blair Warner Apr 14 '16 at 20:59
  • @BlairWarner check if path has non-ASCII characters or spaces http://stackoverflow.com/questions/1981390/urlwithstring-returns-nil – Maciej Gad Apr 14 '16 at 21:06
  • @BlairWarner you could check by adding: `path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];` – Maciej Gad Apr 14 '16 at 21:08
  • @bazyl87 Ok strangely enough, when I do the above, I get this: This is path (%0A%20%20%20%20%22http://url.com/files/stored/1460659054.jpg%22%0A) (EVEN THOUGH when I logged path with NSLog, it doesnt show all of those hidden characters...) How do I fix this? :) – Blair Warner Apr 14 '16 at 21:18
  • @BlairWarner could you replace `path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];` with `NSMutableCharacterSet *characterSetToTrim = [NSMutableCharacterSet characterSetWithCharactersInString:@"()\""]; [characterSetToTrim formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; path = [path stringByTrimmingCharactersInSet:characterSetToTrim]; ` – Maciej Gad Apr 14 '16 at 21:29
  • @bazyl87 WORKED!!! THANK YOU SO MUCH!! – Blair Warner Apr 14 '16 at 21:32

3 Answers3

0

Try like this,

NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
self.dogimageView.image = [[UIImageView alloc] initWithImage:image];

and You can use great library SDWebImage for caching image. When image comes from server use of this library is very helpful. Hope this will work.

Update :

 NSString *path = [NSString stringWithFormat:@"%@", [dic2 valueForKey: @"safe_value"]];
 NSURL *url = [NSURL URLWithString:path];
 NSData *data = [NSData dataWithContentsOfURL:url];
 UIImage *image = [UIImage imageWithData:data];
 self.dogimageView.image = [[UIImageView alloc] initWithImage:image];

Try this. :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

Try using dataWithContentsOfURL:options:error: instead and look if there is any error message there

Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • Logged every line - for some reason ImageURL is where path becomes a null value, and thus imageData is empty. I'm just not sure why ImageURL is coming back null when path is clearly populated? – Blair Warner Apr 14 '16 at 19:41
  • what's your actual URL there? the string...it might have special characters in it. Try to use : [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; – Eugene Gordin Apr 14 '16 at 19:54
0

I recommend usage of AFNetworking https://github.com/AFNetworking/AFNetworking for all networking connect features.

  1. Import UIImageView+AFNetworking.h at beginning of file

    #import "UIImageView+AFNetworking.h"

  2. Get path from dic2

    NSString *path = dic2[@"safe_value"];

  3. Check if path is a string

    if (![path isKindOfClass:[NSString class]]) { return; }

  4. Create a NSURL object from string

    NSURL *imageUrl = [NSURL URLWithString:path];

  5. Set imageUrl to UIImageView

    [self.dogimageView setImageWithURL:imageUrl];

You could check the full code in my gist https://gist.github.com/MaciejGad/fa8cb80dfc73ea55edeedb75418ea2ec


Edit: the reason of error was the path that was set as:

NSString *path = @"(
\"http://url.com/default/files/stored/1460659054.jpg\"
)";

to change to normal URL we need to trim it:

NSMutableCharacterSet *characterSetToTrim = [NSMutableCharacterSet characterSetWithCharactersInString:@"()\""]; 
[characterSetToTrim formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
path = [path stringByTrimmingCharactersInSet:characterSetToTrim];

after this trimming path should be http://url.com/default/files/stored/1460659054.jpg

Maciej Gad
  • 1,701
  • 16
  • 21
  • Tried this - I'm already using AFNetworking as well so it was easy enough to implement. But this line [self.dogimageView sd_setImageWithURL:imageUrl]; ....is crashing my app with the following error: '-[UIImageView sd_setImageWithURL:]: unrecognized selector sent to instance 0x12bff86b0' – Blair Warner Apr 14 '16 at 19:49
  • `AFNetworking` use the name `setImageWithURL` not `sd_setImageWithURL`. Check if you are correctly installed the library. The best way is to use Cocoa Pods https://github.com/AFNetworking/AFNetworking#installation-with-cocoapods . To learn more about Coca Pods visit https://cocoapods.org/ Method `sd_setImageWithURL` is from https://github.com/rs/SDWebImage – Maciej Gad Apr 14 '16 at 19:58
  • Ahh good catch! I'm using AFNetworking as well as SDWebImage - hence the confusion ahaha. App no longer crashes, but my imageUrl is still null (logged this line)? NSURL *imageUrl = [NSURL URLWithString:path]; – Blair Warner Apr 14 '16 at 20:04
  • Could you add line: `NSLog(@"%@, %@", path, NSStringFromClass(path.class));` and check in browser if URL is pointing to image and type of path is `NSString` – Maciej Gad Apr 14 '16 at 20:10
  • @BlairWarner could you accept the answer (I've updated it) – Maciej Gad Apr 14 '16 at 21:39