iPhone動態(tài)加載圖片 實(shí)例講解
iPhone動態(tài)加載圖片 實(shí)例講解是本文介紹的內(nèi)容。不多說了,先來看內(nèi)容。官方的例子(支持3.x以上的機(jī)子)
http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html
其實(shí)在iphone上面是實(shí)現(xiàn)圖片的動態(tài)加載,其實(shí)也不是很難,其中只要在代理中實(shí)現(xiàn)方法就可以,首先在頭文件中聲明使用到的代理:如
- @interface XXX : UIViewController<UIScrollViewDelegate>
然后在.m中實(shí)現(xiàn)
- //滾動停止的時候在去獲取image的信息來顯示在UITableViewCell上面
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
- if (!decelerate)
- {
- [self loadImagesForOnscreenRows];
- }
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- [self loadImagesForOnscreenRows];
- }
- //
- - (void)loadImagesForOnscreenRows
- {
- if ([self.entries count] > 0)
- {
- NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
- for (NSIndexPath *indexPath in visiblePaths)
- {
- AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
- if (!appRecord.appIcon) // avoid the app icon download if the app already has an icon
- {
- [self startIconDownload:appRecord forIndexPath:indexPath];
- }
- }
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- ………//初始化UITableView的相關(guān)信息
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
- reuseIdentifier:CellIdentifier] autorelease];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }
- ……
- if (!appRecord.appIcon)//當(dāng)UItableViewCell還沒有圖像信息的時候
- {
- if (self.tableView.dragging == NO && self.tableView.decelerating == NO)//table停止不再滑動的時候下載圖片(先用默認(rèn)的圖片來代替Cell的image)
- {
- [self startIconDownload:appRecord forIndexPath:indexPath];
- }
- cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
- }
- else//當(dāng)appReacord已經(jīng)有圖片信息的時候直接顯示
- {
- cell.imageView.image = appRecord.appIcon;
- }
- }
以上就是動態(tài)加載的主要的代碼實(shí)現(xiàn)(其中不包括從網(wǎng)絡(luò)上面下載圖片信息等操作)
因?yàn)槲覀儎?chuàng)建UITableviewCell的時候是以重用的方式來創(chuàng)建,所以就相當(dāng)于說***屏顯示的cell就是以后顯示數(shù)據(jù)和圖片的基礎(chǔ),因?yàn)楹竺鏀?shù)據(jù)超出一平的時候,我們只是改變數(shù)據(jù)的顯示,并沒有為每一個cell的數(shù)據(jù)元創(chuàng)建相應(yīng)的一個
UITableViewCell(這樣非常的浪費(fèi)內(nèi)存),要是我們沒有實(shí)現(xiàn)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- 和
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
代理的時候,當(dāng)我們滾動UITableView的時候TableView 就是按照順序來加載圖片的信息資源,這樣當(dāng)我們用力滾動Table的時候就感覺相當(dāng)?shù)目?,(其?shí)UITableView實(shí)在一個個的顯示出cell的信息)
當(dāng)我們實(shí)現(xiàn)了以上代理的話,就可以實(shí)現(xiàn)在tableView滾動停止的時候,在去加載數(shù)據(jù)信息,這樣滾動期間的tableViewCell就可以用默認(rèn)的圖片信息來顯示了。
小結(jié):iPhone動態(tài)加載圖片 實(shí)例講解的內(nèi)容介紹完了,希望本文對你有所幫助!