图像无法加载

时间:2017-12-08 05:52:42

标签: objective-c image url

我将此代码用于endcodeURLString。

url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

在不使用stringByAddingPercentEscapesUsingEncoding的情况下使用关键字之前,
以及关键字在使用stringByAddingPercentEscapesUsingEncoding之后。

它将52添加到URL 2(不工作),但在URL 1中(它正在工作)

网址1.之前=>
https://images-na.ssl-images-amazon.com/images/I/41e3n4R8cUL._SL160_.jpg

网址1.之后=>
https://images-na.ssl-images-amazon.com/images/I/41e3n4R8cUL._SL160_.jpg

网址2.之前=>
https://images-na.ssl-images-amazon.com/images/I/41T%2BHZfQPsL._SL160_.jpg

网址2.之后=>
https://images-na.ssl-images-amazon.com/images/I/41T%252BHZfQPsL._SL160_.jpg

您可以在%2之后的第二个网址中看到它添加了%252。我不知道为什么会发生这种情况或解决方案是什么。

贝娄代码用于cellForItemAtIndexPath

[Common loadPostImg:a_cell.imgProduct :[dictImage valueForKey:@"url"]];

+(void)loadPostImg :(UIImageView *)imgView :(NSString *)strUrl{
if (strUrl)
{
    if (strUrl.length !=0)
    {
        strUrl = [Common appendURL:strUrl];

        [imgView setShowActivityIndicatorView:NO];
        imgView.backgroundColor = [UIColor clearColor];
        NSLog(@"before => %@",strUrl);
        strUrl = [Common endcodeURLString:strUrl];
        NSLog(@"After => %@",strUrl);
        [imgView sd_setImageWithURL:[NSURL URLWithString:strUrl] placeholderImage:[UIImage imageNamed:@"DefaultBanner"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
         {
             [imgView stopAnimating];
         }];

    }
    else
    {
        imgView.image = [UIImage imageNamed:@"DefaultBanner"];
    }
}
else
{
    imgView.image = [UIImage imageNamed:@"DefaultBanner"];
}}

+(NSString *)endcodeURLString :(NSString *)url{
url = [url stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return url;}

1 个答案:

答案 0 :(得分:0)

你可以这样做

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:<Your original URL without percentage escaping>]]];
[self.view addSubview:imageView];

希望这有帮助