IOS 載入網狀圖片的方式對比

來源:互聯網
上載者:User

IOS 載入網狀圖片的方式對比

 //1. NSData dataWithContentsOfURL//    [self.icon setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:tempUrl]]];        //2. dispath形式添加到非同步處理//    [self imageDownLoadByUrlASYNC:tempUrl Complete:^(UIImage *image) {//        [self.icon setImage:image];//    }];    //3. 當前我所選用的方式 邊下載邊載入的方式 用的CGImageRef imageWithCGImage    _request = [[NSURLRequest alloc] initWithURL:tempUrl];    _conn    = [[NSURLConnection alloc] initWithRequest:_request delegate:self];        _incrementallyImgSource = CGImageSourceCreateIncremental(NULL);        _recieveData = [[NSMutableData alloc] init];    _isLoadFinished = false;        self.icon.alpha = .5;    self.lblTitle.alpha = .5;    [self.lblTitle setText:appName];

第一種方式,是基本上很少有人用的 是最基礎的方式 這種方式有個問題 就是網路不好的情況下會卡主線程,導致程式假死

 

第二種方式,請款這段實現代碼

////-(void)imageDownLoadByUrlASYNC:(NSURL *)url Complete:(complete)finished//{//    //非同步並列執行//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//        UIImage *image = nil;//        NSError *error;//        NSData *responseData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];//        image = [UIImage imageWithData:responseData];//        //跳回主隊列執行//        dispatch_async(dispatch_get_main_queue(), ^{//            //在主隊列中進行ui操作//            finished(image);//        });//        //    });//}
雖然情況跟第一種實現一樣,但是將執行代碼添加到對應的非同步執行中 然後再成功下載之後 擷取到image之後 放到主線程執行回調 設定image

 

 

第三種方式 需要以下代碼 這是我百度到的方式

 

#pragma mark -- NSURLConnectionDataDelegate-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{    _expectedLeght=response.expectedContentLength;    NSLog(@"expectedLength:%lld",_expectedLeght);        NSString*mimeType=response.MIMEType;    NSLog(@"MIMETYPE%@",mimeType);        NSArray*arr=[mimeType componentsSeparatedByString:@"/"];    if(arr.count<1||![[arr objectAtIndex:0] isEqual:@"image"])    {        NSLog(@"notaimageurl");        [connection cancel];        _conn=nil;    }}-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error{    NSLog(@"Connection%@error,errorinfo:%@",connection,error);}-(void)connectionDidFinishLoading:(NSURLConnection*)connection{    NSLog(@"ConnectionLoadingFinished!!!");    //ifdownloadimagedatanotcomplete,createfinalimage    if(!_isLoadFinished){        CGImageSourceUpdateData(_incrementallyImgSource,(CFDataRef)_recieveData,_isLoadFinished);        CGImageRef imageRef=CGImageSourceCreateImageAtIndex(_incrementallyImgSource,0,NULL);        UIImage * image=[UIImage imageWithCGImage:imageRef];        [self.icon setImage:image];        CGImageRelease(imageRef);        }}-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{    [_recieveData appendData:data];        _isLoadFinished=false;if(_expectedLeght==_recieveData.length){        _isLoadFinished=true;        }        CGImageSourceUpdateData(_incrementallyImgSource,(CFDataRef)_recieveData,_isLoadFinished);    CGImageRef imageRef=CGImageSourceCreateImageAtIndex(_incrementallyImgSource,0,NULL);    UIImage * image=[UIImage imageWithCGImage:imageRef];    [self.icon setImage:image];    CGImageRelease(imageRef);}

這個方法經過我測試了 非常好用 但是不知道會不會有什麼bug 只是剛使用 並且使用者體驗也會相應增加

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.