iOS loading picture does not display the issue

Source: Internet
Author: User

I. Specific issues

In the process of development, found that some of the screen part of the picture display problems only show the placeholder image, take out the URL of the picture in the browser can open, a variety of attempts even to find friends to help in their projects show there will be problems, Finally found through the third party framework sdwebimage or yywebimage download the URL image with a comma link will download the failure, the download method completed callback block inside print information as follows:

Error domain=nsurlerrordomain code=403"(null)"

There are two image URLs that are not displayed properly:

http://img1.imgtn.bdimg.com/it/u=3044191397,2911599132&fm=27&gp=0.jpghttp: // img2.imgtn.bdimg.com/it/u=3509004173,840437551&fm=27&gp=0.jpg

Interested partners can get their own projects to try

Second, the cause of the problem

The network has a small partner is due to lack of user-agent user agent. Only the user agent is set up to access this URL picture with a comma. As for the format of this user agent, it is possible to have either a value or a specified format string.

Third, the specific solution

1. Third-party framework Yywebimage

Locate the Yywebimagemanager.m file, navigate to the setting of the HTTP request header's properties that is _headers, add a user-agent key value pair, the specific changes can see the following method

-(Instancetype) Initwithcache: (Yyimagecache *) cache queue: (Nsoperationqueue *) queue{ Self=[Super Init]; if(!self)returnNil; _cache=Cache; _queue=queue; _timeout=15.0; NSString*useragent =@""; UserAgent= [NSString stringWithFormat:@"%@/%@ (%@; IOS%@; scale/%0.2f)", [[[NSBundle Mainbundle] infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleexecutablekey]?: [[[NSBundle] Mainbundle] Infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleidentifierkey], [[[[NSBundle MainBundle] Infodictionary] Objectforkey:@"cfbundleshortversionstring"]?: [[[NSBundle Mainbundle] infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleversionkey], [[Uidevice Currentdevice] model], [[Uidevice Currentdevice] systemversion], [[UIScreen        Mainscreen] [scale]]; if(useragent) {if(![useragent canbeconvertedtoencoding:nsasciistringencoding]) {nsmutablestring*mutableuseragent =[useragent mutablecopy]; if(Cfstringtransform (__bridge cfmutablestringref) (mutableuseragent), NULL, (__bridge cfstringref)@"Any-latin; Latin-ascii; [: ^ascii:] Remove",false) ) {useragent=mutableuseragent; }        }    }    //image URL with comma does not display the problem, it is important to set up the agent to resolve    if(Yyimagewebpavailable ()) {_headers= @{@"Accept":@"image/webp,image/*;q=0.8",@"user-agent": useragent}; } Else{_headers= @{@"Accept":@"image/*;q=0.8",@"user-agent": useragent}; }    returnSelf ;}

2. Third-party framework Sdwebimage

Locate the sdwebimagedownloader.m file, and also locate the HTTP request header to set the properties of _httpheaders, add a user-agent key value pair, the specific changes can see the following method

- (ID) init {if(self =[Super Init]) {_operationclass= [Sdwebimagedownloaderoperationclass]; _shoulddecompressimages=YES; _executionorder=Sdwebimagedownloaderfifoexecutionorder; _downloadqueue= [NsoperationqueueNew]; _downloadqueue.maxconcurrentoperationcount=6; _urlcallbacks= [NsmutabledictionaryNew]; /***********************/NSString*useragent =@""; UserAgent= [NSString stringWithFormat:@"%@/%@ (%@; IOS%@; scale/%0.2f)", [[[NSBundle Mainbundle] infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleexecutablekey]?: [[[NSBundle] Mainbundle] Infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleidentifierkey], [[[[NSBundle MainBundle] Infodictionary] Objectforkey:@"cfbundleshortversionstring"]?: [[[NSBundle Mainbundle] infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleversionkey], [[Uidevice Currentdevice] model], [[Uidevice Currentdevice] systemversion], [[UIScreen                Mainscreen] [scale]]; if(useragent) {if(![useragent canbeconvertedtoencoding:nsasciistringencoding]) {nsmutablestring*mutableuseragent =[useragent mutablecopy]; if(Cfstringtransform (__bridge cfmutablestringref) (mutableuseragent), NULL, (__bridge cfstringref)@"Any-latin; Latin-ascii; [: ^ascii:] Remove",false) ) {useragent=mutableuseragent; }            }        }        /***********************/#ifdefSD_WEBP _httpheaders= [@{@"Accept":@"image/webp,image/*;q=0.8", useragent:@"user-agent"} Mutablecopy];#else_httpheaders= [@{@"Accept":@"image/*;q=0.8", useragent:@"user-agent"} Mutablecopy];#endif_barrierqueue= Dispatch_queue_create ("Com.hackemist.SDWebImageDownloaderBarrierQueue", dispatch_queue_concurrent); _downloadtimeout=15.0; }    returnSelf ;}

Or, directly in the UIIMAGEVIEW+WEBCACHE.M file, add the following code at the front of the unified download image portal

- (void) Sd_setimagewithurl: (nsurl *) URL placeholderimage: (UIImage *) Placeholder options: (sdwebimageoptions) Options Progress: (Sdwebimagedownloaderprogressblock) Progressblock Completed: (sdwebimagecompletionblock) Completedblock {/***********************/NSString*useragent =@""; UserAgent= [NSString stringWithFormat:@"%@/%@ (%@; IOS%@; scale/%0.2f)", [[[NSBundle Mainbundle] infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleexecutablekey]?: [[[NSBundle] Mainbundle] Infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleidentifierkey], [[[[NSBundle MainBundle] Infodictionary] Objectforkey:@"cfbundleshortversionstring"]?: [[[NSBundle Mainbundle] infodictionary] Objectforkey: (__bridge NSString *) Kcfbundleversionkey], [[Uidevice Currentdevice] model], [[Uidevice Currentdevice] systemversion], [[UIScreen        Mainscreen] [scale]]; if(useragent) {if(![useragent canbeconvertedtoencoding:nsasciistringencoding]) {nsmutablestring*mutableuseragent =[useragent mutablecopy]; if(Cfstringtransform (__bridge cfmutablestringref) (mutableuseragent), NULL, (__bridge cfstringref)@"Any-latin; Latin-ascii; [: ^ascii:] Remove",false) ) {useragent=mutableuseragent; }} [[Sdwebimagedownloader Shareddownloader] setvalue:useragent Forhttpheaderfield:@"user-agent"]; }    /***********************/[self sd_cancelcurrentimageload]; ........ Omit original source}

3. Frames for other third parties to download pictures

The direct global search string "accept", because although the lack of settings user-agent user agent, but the HTTP request header will generally have set "accept", so after positioning, directly add a user-agent key value pair can be

iOS loading picture does not display the issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.