Iphone 图片设置阴影和动态加载图片

来源:互联网 发布:淘宝买av种子要搜什么 编辑:程序博客网 时间:2024/05/08 19:58
Iphone 图片设置阴影 
   #import <QuartzCore/QuartzCore.h>
  1. (void)viewDidLoad  
  2.  
  3.     [super viewDidLoad];  
  4.     UIImage *img [UIImage imageNamed:@"Icon.png"];  
  5.     UIImageView *imgView [[UIImageView alloc] initWithImage:img];  
  6.     imgView.center self.view.center;  
  7.     [[imgView layer] setShadowOffset:CGSizeMake(5, 5)]; // 阴影的范围  
  8.     [[imgView layer] setShadowRadius:2];                // 阴影扩散的范围控制  
  9.     [[imgView layer] setShadowOpacity:1];               // 阴影透明度  
  10.     [[imgView layer] setShadowColor:[UIColor brownColor].CGColor]; // 阴影的颜色  
  11.     [self.view addSubview:imgView];  }  

iPhone动态加载图片

 (2011-12-07 10:39:08)
转载
标签: 

杂谈

 

iPhone动态加载图片 实例讲解是本文介绍的内容。不多说了,先来看内容。官方的例子(支持3.x以上的机子)

http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

其实在iphone上面是实现图片的动态加载,其实也不是很难,其中只要在代理中实现方法就可以,首先在头文件中声明使用到的代理:如 

  1. @interface XXX UIViewController<UIScrollViewDelegate> 

然后在.m中实现

  1. //滚动停止的时候在去获取image的信息来显示在UITableViewCell上面  
  2. (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate  
  3. {  
  4.     if (!decelerate)  
  5. {  
  6.         [self loadImagesForOnscreenRows];  
  7.     }  
  8. }  
  9. (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  10. {  
  11.     [self loadImagesForOnscreenRows];  
  12. }  
  13. //  
  14. (void)loadImagesForOnscreenRows  
  15. {  
  16.     if ([self.entries count] > 0)  
  17.     {  
  18.         NSArray *visiblePaths [self.tableView indexPathsForVisibleRows];  
  19.         for (NSIndexPath *indexPath in visiblePaths)  
  20.         {  
  21.             AppRecord *appRecord [self.entries objectAtIndex:indexPath.row];  
  22.             if (!appRecord.appIcon) // avoid the app icon download if the app already has an icon  
  23.             {  
  24.                 [self startIconDownload:appRecord forIndexPath:indexPath];  
  25.             }  
  26.         }  
  27.     }  
  28. }  
  29. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  30. {  
  31. ………//初始化UITableView的相关信息  
  32.      UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  33.  
  34.     if (cell == nil)  
  35. {  
  36.         cell [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  37.  
  38.   reuseIdentifier:CellIdentifier] autorelease];  
  39. cell.selectionStyle UITableViewCellSelectionStyleNone;  
  40.     }  
  41. ……  
  42.      if (!appRecord.appIcon)//当UItableViewCell还没有图像信息的时候  
  43.         {  
  44.             if (self.tableView.dragging == NO && self.tableView.decelerating == NO)//table停止不再滑动的时候下载图片(先用默认的图片来代替Cell的image)  
  45.             {  
  46.                 [self startIconDownload:appRecord forIndexPath:indexPath];  
  47.             }  
  48.             cell.imageView.image [UIImage imageNamed:@"Placeholder.png"];                  
  49.         }  
  50.         else//当appReacord已经有图片信息的时候直接显示  
  51.         {  
  52.    cell.imageView.image appRecord.appIcon;  
  53.           

以上就是动态加载的主要的代码实现(其中不包括从网络上面下载图片信息等操作)

因为我们创建UITableviewCell的时候是以重用的方式来创建,所以就相当于说第一屏显示的cell就是以后显示数据和图片的基础,因为后面数据超出一平的时候,我们只是改变数据的显示,并没有为每一个cell的数据元创建相应的一个

UITableViewCell(这样非常的浪费内存),要是我们没有实现

  1. (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  2. 和  
  3. (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 

代理的时候,当我们滚动UITableView的时候TableView 就是按照顺序来加载图片的信息资源,这样当我们用力滚动Table的时候就感觉相当的卡,(其实UITableView实在一个个的显示出cell的信息)

当我们实现了以上代理的话,就可以实现在tableView滚动停止的时候,在去加载数据信息,这样滚动期间的tableViewCell就可以用默认的图片信息来显示了。

iPhone动态加载图片 实例讲解


原创粉丝点击