现在的位置: 首页 > 综合 > 正文

读layzytableimage的示范程序

2013年07月02日 ⁄ 综合 ⁄ 共 3947字 ⁄ 字号 评论关闭

1. 不具备翻页的功能,

2. 数据量/cell中包含图片(会导致内存使用上升很快)

Main函数没啥特别的:

    NSAutoreleasePool *pool = [[NSAutoreleasePool allocinit];

    int retVal = UIApplicationMain(argc, argv, nilnil);

    [pool release];

    return retVal;  ===》非对象类型,

这个程序提供了一个default。png,居然是个空白的table的背景图!

Mainwindow的内容,一个object设置为appdelegate,一个navigation controller,内含一个rootviewcontroller(就是另外一个nib文件)。构成了启动界面。

static NSString *const TopPaidAppsFeed =

@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/xml";

这个可以用来调整数据测试,比如选500/limit=100//

一些变量,有NSOperationQueue,也有NSMutableData

@property (nonatomic, retain) NSMutableArray *appRecords;

@property (nonatomic, retain) NSOperationQueue *queue;

@property (nonatomic, retain) NSURLConnection *appListFeedConnection;
@property (nonatomic, retain) NSMutableData *appListData;

设置全局的网络连接状态,在状态拦左上位置

   // show in the status bar that network activity is starting

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

异步调用结束后,看到的一些关键处理函数:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.appListFeedConnection = nil;   // release our connection
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  //停止网络状态显示 
    
    // create the queue to run our ParseOperation
    self.queue = [[NSOperationQueue alloc] init];  //建立operatoinqueue,处理xml内容在后台thread
    
    // create an ParseOperation (NSOperation subclass) to parse the RSS feed data so that the UI is not blocked
    // "ownership of appListData has been transferred to the parse operation and should no longer be
    // referenced in this thread.
    //
    ParseOperation *parser   //声明operation,然后放入queue中。。。。
    = [[ParseOperation alloc] initWithData:appListData delegate:self]; //数据传递
    
    [queue addOperation:parser]; // this will start the "ParseOperation",这样就启动了一个线程,然后。。。
    
    [parser release];
    
    // ownership of appListData has been transferred to the parse operation
    // and should no longer be referenced in this thread
    self.appListData = nil;  //防止继续使用该引用
}

为什么,什么是ParseOperation?至少应该是NSopration的一种,检查其定义:

@interface ParseOperation : NSOperation <NSXMLParserDelegate>  确实继承自NSoperation,同时要求实现xml解析的接口。

@protocol ParseOperationDelegate
- (void)didFinishParsing:(NSArray *)appList;
- (void)parseErrorOccurred:(NSError *)error;
@end

在Appdelegate中,我们使用了ParseOperation,当后台执行完成后,需要让前台更新界面。。。。后台线程调用到XXdidFinishParsing,但需要操作UI,所以使用了

performSelectorOnMainThread:@selector(handleLoadedApps:)

- (void)handleLoadedApps:(NSArray *)loadedApps

{

    [self.appRecords addObjectsFromArray:loadedApps];

    

    // tell our table view to reload its data, now that parsing has completed

    [rootViewController.tableView reloadData];

}

#pragma mark -ParseOperation protocol implementation

// -------------------------------------------------------------------------------

// didFinishParsing:appList

// -------------------------------------------------------------------------------

- (void)didFinishParsing:(NSArray *)appList

{

    [self performSelectorOnMainThread:@selector(handleLoadedApps:) withObject:appList waitUntilDone:NO];

    

    self.queue = nil;
  
// we are finished with the queue and our ParseOperation

}

下面是控制显示图片大小的代码:48X48的图片大小

  if (image.size.width != kAppIconHeight && image.size.height != kAppIconHeight)

{

        CGSize itemSize = CGSizeMake(kAppIconHeightkAppIconHeight);

UIGraphicsBeginImageContext(itemSize);

CGRect imageRect = CGRectMake(0.00.0, itemSize.width,
itemSize.height);

[image drawInRect:imageRect];

self.appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//        //把图片画在特定的区域里面

    }

    else

    {

        self.appRecord.appIcon = image;

    }

接下来看tableview自己,两个东西:1是不是初始化的时候,table datasource count就是小的数目,还是数组原样大;2:图片是不是用了单独数组,如果下载后,是不是就加载在内存,还是及时释放的。

@interface RootViewController : UITableViewController <UIScrollViewDelegateIconDownloaderDelegate>

{

NSArray *entries;   // the main data model for our UITableView

    NSMutableDictionary *imageDownloadsInProgress;  // the set of IconDownloader objects for each app 一组下载图片对象

}

//实现协议 icondownloaderDelegate

- (void)appImageDidLoad:(NSIndexPath *)indexPath;

@end

makeObjectsPerformSelector 和performselectorONmainthread的关系。。。。

这个程序使用的内存随着列表数目增加而大量增加,75个多已经有了50M的内存使用了。 那么采用page的那种tableview会不会好点呢??

除非你构造的数组不保存图片信息,就保存image url信息

 

抱歉!评论已关闭.