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

首頁 > 網站 > 建站經驗 > 正文

ios,通過按鈕點擊異步加載圖片

2019-11-02 15:37:38
字體:
來源:轉載
供稿:網友

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

  比較原始的方法:

  代碼如下:

  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:

神奇影院[www.aikan.tv/special/shenqiyingyuan/]
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

  方法二:

  復制代碼 代碼如下:

  @interface UIButton (AsyncImage)

  //size by point

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

  @end

  @implementation UIButton (AsyncImage)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屏山县| 化德县| 承德市| 忻州市| 镇坪县| 新兴县| 贺兰县| 德江县| 蓬溪县| 藁城市| 贵州省| 鄂托克前旗| 湟中县| 登封市| 卓尼县| 长治市| 神池县| 松溪县| 平遥县| 延安市| 永靖县| 丰顺县| 屯留县| 莱芜市| 梧州市| 绥中县| 冷水江市| 宁化县| 陆丰市| 赞皇县| 宝清县| 浮梁县| 方城县| 大厂| 清苑县| 酒泉市| 集安市| 思南县| 涿鹿县| 唐海县| 巴里|