国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 黔江区| 宣恩县| 无极县| 喀什市| 伊川县| 青岛市| 藁城市| 大新县| 延寿县| 重庆市| 泗洪县| 田林县| 加查县| 宁都县| 高州市| 广昌县| 全南县| 青田县| 合山市| 仪征市| 涟水县| 宣化县| 平罗县| 云南省| 新昌县| 美姑县| 萝北县| 巩义市| 东平县| 普格县| 遵义县| 丰原市| 大连市| 祁门县| 迁西县| 屯昌县| 和田县| 黎川县| 富顺县| 彰化县| 杨浦区|