iOS launcher diagram-GIF images obtained from the network, which have been not animated locally, are also blurred

Source: Internet
Author: User

Background introduction:
App launch page, often static graph plus links, gif plus links, short video and other forms.
Our app has only static diagram this one, the function has been realized.
After that, with the need to add gif, it is supposed to add a type of judgment, according to the data type, the content can be displayed in different ways, but never.
Out of the question, the good GIF, the content type yes but the content displayed by the corresponding GIF method is always a static diagram and is still blurred.
Because the previous download pictures, as well as the logic to display the picture is not a problem, so the location of the problem in the display GIF method, so took a detour, but this detour is bound to go.

Below begins our star sea, our goal is to end the problem
First look at the source-the original code

/** * Download new pictures*/+ (void) Downloadadimagewithurl: (NSString *) imageUrl imageName: (NSString *) imageName Imglinkurl: (NSString *) Imglinkurl Imgdeadline: (NSString *) imgdeadline imgstartline: (NSString *) imgstartline{Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{NSData *data =[NSData Datawithcontentsofurl:[nsurl Urlwithstring:imageurl]; UIImage *image =[UIImage Imagewithdata:data]; NSString *filepath = [self getfilepathwithimagename:imagename];//Name of the saved file [UIImageJPEGRepresentation (Image,0) Writetofile:filepath Atomically:yes];if ([UIImageJPEGRepresentation (Image,0) Writetofile:filepath Atomically:yes]) {//Saved successfully//Determine the name of the saved image and the local sandbox in the picture is consistent, if not consistent, the picture has been updated, the first delete the old picture in the sandbox, if the consistent description is to delete the cache after downloading again, then do not need to delete the operation, or cannot find the saved pictureif (![ImageName isequaltostring:[[nsuserdefaults Standarduserdefaults] objectforkey:adimagename]) {[Self deleteoldimage];} [[Nsuserdefaults Standarduserdefaults] Setvalue:imagename forkey:adimagename]; [[Nsuserdefaults Standarduserdefaults] Setvalue:imageurl Forkey:adurl]; [[Nsuserdefaults Standarduserdefaults] Setvalue:imgdeadline forkey:addeadline]; [[Nsuserdefaults Standarduserdefaults] Setvalue:imgstartline forkey:adstartline];// Save picture // [[Nsuserdefaults standarduserdefaults] setvalue:@ "" forkey:@ "Deadline"];  [[Nsuserdefaults standarduserdefaults] setvalue:@ "" forkey:@ "startline"]; [[Nsuserdefaults standarduserdefaults] synchronize]; // write now }else{NSLog (@ " save failed ");});       } 
/** *  */
-(void) Setimgfilepath: (NSString *) imgfilepath{      _imgfilepath = Imgfilepath;    if ([_imgfilepath hassuffix:@ "gif"]) {_adimageview.image = [UIImage sd_animatedgifwithdata: [NSData Datawithcontentsoffile:imgfilepath]]; }Else{_adimageview.image = [UIImage imagewithcontentsoffile:_imgfilepath];}}    

The above two methods one implementation of the download, an implementation of the display, at first, only a picture to do the start-up diagram, this writing reluctantly, although the detour, but will not affect the implementation of the effect
But the new requirement is that GIF needs to be loaded, and maybe a little video later


It's a problem.
GIF has not been downloaded, at first modified the method, after all, GIF 3M is not small, so change the download method, as follows

/** *  */
   NSData *data = [NSData datawithcontentsofurl:[nsurl urlwithstring:imageurl];   UIImage *image = [UIImage imagewithdata:data]; 


Then change to the download method below, for a slightly larger file download

[Nsurlconnection sendasynchronousrequest:[nsurlrequest Requestwithurl:[nsurl Urlwithstring:imageurl]]                                       queue:[ Nsoperationqueue Mainqueue]                           completionhandler:^ (nsurlresponse *response, NSData *data, NSError *  Connectionerror) {                               if (!  Connectionerror) {                                                                                        else {}}];    

But the download solved the problem, found that it is not normal display, but the other parts of the project is not a problem ah, so has been entangled in the following method of the map, in fact, it is completely no problem, just can data is wrong

_adimageview.image = [UIImage sd_animatedgifwithdata:[nsdata Datawithcontentsoffile:imgfilepath]];

Reading data is no problem, only the time to save the data is wrong

It's time to notice that the problem lies
The reason is that the download of the picture resource is not directly saved to a path, but first turned into a picture, and then the image to the data saved, superfluous, the important thing is, GIF map, the original resources have changed, when used, the resources to obtain natural problems, we look at the code

        NSData *data = [NSData datawithcontentsofurl:[nsurl urlwithstring:imageurl];        UIImage *image = [UIImage imagewithdata:data];                        // Save file name        00) writetofile:filepath Atomically:yes]) {}     

This is the storage function code in the download image method: Get data, go image,image write Data save. We can see one more step, maybe the picture, although the picture resource has not changed, but the GIF graph uses [uiimagejpegrepresentation (image, 0) Writetofile:filepath Atomically:yes]; This method of data resources is changed. So the picture is no problem, but GIF is not!

In fact, it doesn't matter what type of resources he has to save data directly.

The following is the modified code

+ (void) Downloadadimagewithurl: (NSString *) imageUrl imageName: (NSString *) imageName Imglinkurl: (NSString *) Imglinkurl Imgdeadline: (NSString *) imgdeadline imgstartline: (NSString *) imgstartline{[nsurlconnection sendasynchronousrequest:[nsurlrequest requestwithurl:[nsurl URLWITHSTRING:IMAGEURL ]] Queue:[nsoperationqueue Mainqueue] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *Connectionerror) {if (!Connectionerror) {NSString *filepath = [Self getFile Pathwithimagename:imagename];//Save the name of the file//Image data stored directly, no need to go to image and then to dataIf([Data Writetofile:filepath Atomically:yes]) {//Saved successfully//Determine the name of the saved image and the local sandbox in the picture is consistent, if not consistent, the picture has been updated, the first delete the old picture in the sandbox, if the consistent description is to delete the cache after downloading again, then do not need to delete the operation, or cannot find the saved pictureif (![ImageName isequaltostring:[[nsuserdefaults Standarduserdefaults] objectforkey:adimagename]) {[Self deleteoldimage];} [[Nsuserdefaults Standarduserdefaults] Setvalue:imagename forkey:adimagename]; [[Nsuserdefaults Standarduserdefaults] Setvalue:imageurl Forkey:adurl]; [[Nsuserdefaults Standarduserdefaults] Setvalue:imgdeadline forkey:addeadline]; [[Nsuserdefaults Standarduserdefaults] Setvalue:imgstartline forkey:adstartline];// save picture // [[Nsuserdefaults Standarduserdefaults] setvalue:@" "forkey:@" Deadline "]; // [[Nsuserdefaults Standarduserdefaults] setvalue:@ "" forkey:@ "Startline"]; [[Nsuserdefaults standarduserdefaults] synchronize]; // write }else{NSLog (@"  save failed  ");} else {}}]; return;}        

After testing, GIF is displayed normally ... Problem solved!

In summary, the problem is always where you think it's impossible to go wrong!

Author Wandering

View Articles



iOS launcher diagram-GIF images obtained from the network, which have been not animated locally, are also blurred

Related Article

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.