国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

ios通過(guò)按鈕 點(diǎn)擊異步加載圖片

2019-11-02 14:40:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

   本文給大家匯總了幾種IOS中實(shí)現(xiàn)異步加載圖片的方法,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。

  比較原始的方法:

  代碼如下:

  AsyncImageView.h:

  #import

  @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

  方法二:

  復(fù)制代碼 代碼如下:

  @interface UIButton (AsyncImage)

  //size by point

  - (void)setImageFromURL:(NSString *)urlString adjustToSize:(CGSize)size completion:(void (^)(void))completion logo:(UIImage *)logoImage;

  @end

  @implementation UIButton (AsyncImage)

  - (void)setImageFromURL:(NSString *)urlString adjustToSize:(CGSize)size completion:(void (^)(void))completion logo:(UIImage *)logoImage

  {

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIOR

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南木林县| 罗源县| 铁岭县| 海林市| 五寨县| 绥棱县| 常熟市| 玉林市| 宁蒗| 雷波县| 绥宁县| 景泰县| 桓台县| 芜湖市| 盈江县| 信宜市| 新乡市| 毕节市| 巢湖市| 临漳县| 藁城市| 菏泽市| 大英县| 静宁县| 农安县| 广水市| 辉县市| 四平市| 信宜市| 密云县| 柘荣县| 屏东县| 海阳市| 肥东县| 吉林市| 锡林郭勒盟| 乌拉特中旗| 莱芜市| 福海县| 桃江县| 关岭|