iOS開發之非同步載入圖片

來源:互聯網
上載者:User
比較原始的方法:AsyncImageView.h:

#import <UIKit/UIKit.h>

@interface AsyncImageView : UIView

{

    NSURLConnection* connection;

    NSMutableData* data;

}

- (void)loadImageFromURL:(NSURL*)url;

@end

AsyncImageView.m:

#import "AsyncImageView.h"

@implementation AsyncImageView

  

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self) {

        // Initialization code

    }

    returnself;

}

  

- (void)loadImageFromURL:(NSURL*)url {

    if(connection!=nil) { [connection release]; }

    if(data!=nil) { [data release]; }

    NSURLRequest* request = [NSURLRequest requestWithURL:url

                                             cachePolicy:NSURLRequestUseProtocolCachePolicy

                                         timeoutInterval:60.0];

    connection = [[NSURLConnection alloc]

                  initWithRequest:request delegate:self];

}

  

- (void)connection:(NSURLConnection *)theConnection

    didReceiveData:(NSData *)incrementalData {

    if(data==nil) {

        data =

        [[NSMutableData alloc] initWithCapacity:2048];

    }

    [data appendData:incrementalData];

}

  

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

  

    [connection release];

    connection=nil;

  

    if([[self subviews] count] > 0) {

        [[[self subviews] objectAtIndex:0] removeFromSuperview];

    }

  

    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];

  

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );

  

    [self addSubview:imageView];

    imageView.frame = self.bounds;

    [imageView setNeedsLayout];

    [self setNeedsLayout];

    [data release];

    data=nil;

}

  

- (UIImage*) image {

    UIImageView* iv = [[self subviews] objectAtIndex:0];

    return[iv image];

}

  

- (void)dealloc {

    [connection cancel];

    [connection release];

    [data release];

    [super dealloc];

}

@end

使用:

AsyncImageView *asyncImage = [[AsyncImageView alloc] init];

asyncImage.frame = CGRectMake(100, point_y, 95, 95)

//圓角

[asyncImage.layer setMasksToBounds:YES];

[asyncImage.layer setCornerRadius:15];

[asyncImage loadImageFromURL:[NSURL URLWithString:@"www.istar.name/...."]];

不過發現一個好東東, SDWebImage, 這個實在是太方便了
首頁:https://github.com/rs/SDWebImage
1.下載下來放到project裡面
2. 添加:MapKit.framework
3. #import “UIImageView+WebCache.h”
4. 使用:

UIImageView *asyncImage = [[UIImageView alloc] init];

[asyncImage setImageWithURL:[NSURL URLWithString:@www.istar.name/....]];

 

使用第三方庫

EGOImageLoading是我在項目中用的比較多的一個第三方圖片非同步載入類,你們可以在git上找到並下載它,連結如下。另外提一下,
廣為人知的下拉重新整理EGORefreshTableHeaderView也是就是這個人寫的。

https://github.com/enormego/EGOImageLoading
(下載後運行demo程式XCode會提示找不到EGOCache.h標頭檔,可以在這個地方下載https://github.com/enormego/EGOCache)

使用方法可以參照裡面的demo程式,很簡單,只要把ImageUrl告訴它,剩下的就什麼都不用管了,它會幫你非同步載入,還會做緩衝處理...

?1234     // 設定預設佔位圖片     myEgoimageView.placeholderImage = [UIImage imageNamed:@"placeholder.png"];     // 告訴它圖片的url地址, done     myEgoimageView.imageURL = [NSURL URLWithString:@"http://simg.cocoachina.com/201111220746561330.jpg"];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.