【问题标题】:UIImageView from JSON来自 JSON 的 UIImageView
【发布时间】:2016-06-01 13:33:52
【问题描述】:

来自 JSON 文件的数据。

 jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSString *cPhoto = [[jsonArray objectAtIndex:i] objectForKey:@"photo"]; 
                NSURL *urlOne = [NSURL URLWithString:cPhoto];

                NSData *photoData =[NSData dataWithContentsOfURL:urlOne];
                UIImage *imageOne = [[UIImage alloc] initWithData:photoData];

                NSLog(@"%@",imageOne);

NSLog 视图:

    [10872:310156] (null)
    [10872:310156] <UIImage: 0x7f97f25cd220>, {1005, 651}
    [10872:310156] <UIImage: 0x7f97f25d59c0>, {1005, 651}
    [10872:310156] (null)
    [10872:310156] (null)

为什么 NSLog 不显示 *.jpg 格式? .仅 *.png 文件(2-nd 和 3-rd)。 非常感谢。原谅我的英语不好!

【问题讨论】:

  • photoData 是否有效是导致问题的原因?网址有效吗?
  • @Larme 当然可以。除了照片获取数据类型编号、姓名、姓氏...
  • 我的意思是,当UIImage 为 nil 时,photoData 也为 null 吗?
  • @Larme 是的。 UIImage 仅显示照片 .png 格式
  • 如果NSData 为空,那么来自NSData 对象的UIImage 也为空是正常的。 jpg 的photoData 是否获得正确的文件开头:en.wikipedia.org/wiki/JPEG#Syntax_and_structure

标签: objective-c json string uiimageview nslog


【解决方案1】:

试试下面的答案

UIImage* imageOne = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:cPhoto]]];

【讨论】:

  • 三行合二为一。这应该如何解决这个问题?
  • 绝对能帮到你。
  • 先应用此编码。
  • 您介意解释一下 OP 的代码和您的代码之间的区别吗?OP 的代码有什么问题?
猜你喜欢
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多