【问题标题】:How To convert URL link into UIImage in iOS?如何在 iOS 中将 URL 链接转换为 UIImage?
【发布时间】:2014-07-09 10:49:07
【问题描述】:

我正在使用 UIImage 视图并像此代码一样在其中添加图像

 NSURL *imageURL = [NSURL URLWithString:@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001 123.jpg"];
NSLog(@"imge %@",imageURL);

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    NSLog(@"imge %@",imageData);
UIImage *image = [UIImage imageWithData:imageData];
   NSLog(@"imge %@",image);
[image_view setImage:image];

但是所有的 nslog 都显示 null 实际上 url 有问题但是这个在网络上显示图像所以请告诉我这里有什么问题。

【问题讨论】:

    标签: ios objective-c uiimage


    【解决方案1】:

    尝试以下代码:

    NSURL *aURL = [NSURL URLWithString:[@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001%20123.jpg" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    
    UIImage *aImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];
    

    这工作正常...!!!

    【讨论】:

      【解决方案2】:

      您的 URL 字符串有一个空格,需要用百分比转义符替换。

      要么使用这个 -

      NSURL *imageURL = [NSURL URLWithString:@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001%20123.jpg"];
      

      或者

      NSString *imageUrlString = @"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001 123.jpg";
      NSString *encodedImageUrlString = [imageUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      NSURL *imageURL = [NSURL URLWithString: encodedImageUrlString];
      

      【讨论】:

      • 如果您认为这是重复的,那么您应该将其标记为重复,而不仅仅是说它在您的答案中。我已删除此评论。
      猜你喜欢
      • 2013-11-13
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 2012-10-23
      • 2015-04-22
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多