1. 程式人生 > >iOS啟動圖-從網絡獲取的gif圖,在本地一直是沒有動畫,還模糊的

iOS啟動圖-從網絡獲取的gif圖,在本地一直是沒有動畫,還模糊的

ads dad microsoft view text isequal sof nload conn

背景介紹:
APP啟動頁,常有靜態圖加鏈接,gif加鏈接,短視頻等幾種形式。
我們APP前期只有靜態圖這一種,功能已經實現。
之後,有了添加gif的需求,按理說,只要添加一個類型判斷,按照數據類型,通過不同方法展示內容即可,但是一直不可以。。
出了這樣的問題,下好的gif圖,內容類型沒錯但是通過對應的gif方法顯示的內容一直是一張靜態圖,並且還是模糊的。
因為之前的下載圖片,以及顯示圖片的邏輯完全沒問題,所以定位問題在顯示gif的方法上,所以走了彎路,但是這條彎路是必然要走的。

下面開始我們的星辰大海,我們的目標是終結問題
先看源碼--原來的代碼

/**
 *  下載新的圖片
 */
+ (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]; // 保存文件的名稱
        [UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES];
        if ([UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES]) {
            
            // 保存成功
            //判斷保存下來的圖片名字和本地沙盒中存在的圖片是否一致,如果不一致,說明圖片有更新,此時先刪除沙盒中的舊圖片,如果一致說明是刪除緩存後再次下載,這時不需要進行刪除操作,否則找不到已保存的圖片
            if (![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];
            //保存圖片
//            [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"Deadline"];
//            [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"startline"];
            [[NSUserDefaults standardUserDefaults] synchronize];//立即寫入
            
        }else{
            NSLog(@"保存失敗");
        }
        
    });
}
/**
 *  通過在沙盒路徑,獲取gif圖
 */
-(void)setImgFilePath:(NSString *)imgFilePath{
      _imgFilePath = imgFilePath;
    if ([_imgFilePath hasSuffix:@"gif"]) {
         _adImageView.image = [UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfFile:imgFilePath]];
    }else{
         _adImageView.image = [UIImage imageWithContentsOfFile:_imgFilePath];
    }
  
   
}

以上兩個方法一個實現下載,一個實現顯示,起初只有一張圖片做啟動圖的時候,這種寫法勉強用,雖然走了彎路,但是不會影響實現效果
但是新需求是需要加載gif,或許以後還有小視頻


問題出來了
gif一直下載不下來,起初修改了方法,畢竟gif有3M不小,所以換了下載方法,如下

/**
 *  這個方法,下圖片還行,你要是下載個大點的gif那就不合適了
 */
   NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
   UIImage *image = [UIImage imageWithData:data];


那就改成下邊的下載方法,適合稍大一點的文件下載

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]]
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if (!connectionError) {
                                   
                      
                               } else {
                                   
                               }
                           }];

但是下載解決了問題後,發現還不能正常顯示,但是項目中其他地方是沒問題的啊,所以一直糾結於下面這個賦圖的方法,其實是完全沒問題的,只是能數據就不對了

 _adImageView.image = [UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfFile:imgFilePath]];

讀取數據沒問題,只能是存數據的時候不對了

這個時候註意到,問題所在
原因是下載的圖片資源沒有直接保存到某個路徑下,而是先轉成圖片,然後圖片轉data保存了,多此一舉了,重要的是,gif圖的話,就把原來的資源都改變了,用的時候,獲取的資源自然就出問題了,我們看代碼

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        UIImage *image = [UIImage imageWithData:data];
        
        
        NSString *filePath = [self getFilePathWithImageName:imageName]; // 保存文件的名稱
        [UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES];
        if ([UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES]){}

這是下載圖片方法中的存儲功能代碼:獲取data,轉image,image寫成data保存。我們可以看到多了一步,可能圖片的時候,雖然多轉換一步,圖片資源沒有改變,但是gif圖用[UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES];這個方法數據資源就改變了。所以導致圖片雖然沒問題,但是gif就不行了!

這兒其實不管他是何種類型資源直接保存data就好了。

下面是修改後的代碼

+ (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 getFilePathWithImageName:imageName]; // 保存文件的名稱
    
                                   //圖片data直接存儲,不需要轉image然後再轉data
                                   if ([data writeToFile:filePath atomically:YES]) {
                                       
                                       // 保存成功
                                       //判斷保存下來的圖片名字和本地沙盒中存在的圖片是否一致,如果不一致,說明圖片有更新,此時先刪除沙盒中的舊圖片,如果一致說明是刪除緩存後再次下載,這時不需要進行刪除操作,否則找不到已保存的圖片
                                       if (![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];
                                       //保存圖片
                                       //            [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"Deadline"];
                                       //            [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"startline"];
                                       [[NSUserDefaults standardUserDefaults] synchronize];//立即寫入
                                       
                                   }else{
                                       NSLog(@"保存失敗");
                                   }
 
                                   
                                   
                               } else {
                                   
                               }
                           }];
    return;
}

測試之後,正常顯示gif。。。問題得以解決!

總結,問題往往就在你以為不可能出錯的地方!

作者流浪

查看文章



iOS啟動圖-從網絡獲取的gif圖,在本地一直是沒有動畫,還模糊的