iOS 实现大图片下载中逐渐显示

iOS 实现大图片下载中逐渐显示_第1张图片
效果

通过imageIO实现图片的渐进加载

创建一个URLConnnection,然后实现代理,每次接收到数据时更新图片,代码实现:

#import "LXGIncreasImage.h"
#import 
#import 

@interface LXGIncreasImage () {
    NSURLRequest    *_request;
    NSURLConnection *_connect;
    
    CGImageSourceRef _incrementallyImgSource;
    
    NSMutableData   *_recieveData;
    long long       _expectedLeght;
    BOOL            _isLoadFinished;
}

@end

@implementation LXGIncreasImage
- (id)initWithURL:(NSURL *)imageURL
{
    self = [super init];
    if (self) {
        
        _request = [[NSURLRequest alloc] initWithURL:imageURL];
        
        _connect = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
        _incrementallyImgSource = CGImageSourceCreateIncremental(NULL);
        
        _recieveData = [[NSMutableData alloc] init];
        _isLoadFinished = false;
    }
    
    return self;
}

#pragma mark -
#pragma mark NSURLConnectionDataDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _expectedLeght = response.expectedContentLength;
    NSLog(@"iamge Length: %lld", _expectedLeght);
    
    NSString *mimeType = response.MIMEType;
    NSLog(@"MIME TYPE %@", mimeType);
    
    NSArray *arr = [mimeType componentsSeparatedByString:@"/"];
    if (arr.count < 1 || ![[arr objectAtIndex:0] isEqual:@"image"]) {
        NSLog(@"not a image url");
        [connection cancel];
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection %@ error, error info: %@", connection, error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Connection Loading Finished!!!");
    
    // if download image data not complete, create final image
    if (!_isLoadFinished) {
        CGImageSourceUpdateData(_incrementallyImgSource, (CFDataRef)_recieveData, _isLoadFinished);
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_incrementallyImgSource, 0, NULL);
        self.image = [UIImage imageWithCGImage:imageRef];
        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);
    self.image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
}


@end

调用代码:

    _iamge = [[LXGIncreasImage alloc] initWithFrame:CGRectMake(10, 50, 300, 400)];
    _iamge.backgroundColor = [UIColor lightGrayColor];
    _iamge.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.view addSubview:_iamge];

- (IBAction)lauchImage:(id)sender {
    
    [_iamge initWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488975368399&di=1f23b322e6d0539c52aac156af5e32da&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F96dda144ad3459820fd47bfa0af431adcbef8478.jpg"]];
}

你可能感兴趣的:(iOS 实现大图片下载中逐渐显示)