使用URL设置时,imageView为空?

时间:2016-04-14 18:59:22

标签: ios objective-c

我使用下面的代码来设置我的imageView(位于路径中)。是的,URL数据成功返回,但由于某种原因,我的imageView仍为空白(只是白色?)

是的,我已经连接了我的imageView属性......知道什么是错的?我应该使用不同的代码块来实现这一目标吗?

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

路径:

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

NSLog(@"这是路径%@,%@",路径,NSStringFromClass(path.class)); 返回:

2016-04-14 13:18:39.590 [5225:1700657] This is path (
    "http://url.com/default/files/stored/1460659054.jpg"
), __NSCFString

3 个答案:

答案 0 :(得分:0)

试试这个,

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

并且您可以使用优秀的库SDWebImage来缓存图片。当图像来自服务器时,使用此库非常有帮助。希望这会奏效。

更新:

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

试试这个。 :)

答案 1 :(得分:0)

尝试使用dataWithContentsOfURL:options:error:而不是查看是否有任何错误消息

答案 2 :(得分:0)

我建议对所有网络连接功能使用AFNetworking https://github.com/AFNetworking/AFNetworking

  1. 在文件开头

    导入UIImageView + AFNetworking.h

    #import "UIImageView+AFNetworking.h"

  2. 从dic2获取路径

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

  3. 检查路径是否为字符串

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

  4. 从字符串

    创建NSURL对象

    NSURL *imageUrl = [NSURL URLWithString:path];

  5. imageUrl设为UIImageView

    [self.dogimageView setImageWithURL:imageUrl];

  6. 您可以查看我的要点https://gist.github.com/MaciejGad/fa8cb80dfc73ea55edeedb75418ea2ec

    中的完整代码

    编辑: 错误的原因是设置为:

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

    要更改为普通网址,我们需要修剪它:

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

    此修剪路径应为http://url.com/default/files/stored/1460659054.jpg