The IOS learning nsoperation network downloads pictures involving plist files, reusing cells, taking up bitmaps, caching pools, cleaning up memory __ios

Source: Internet
Author: User

#import "HomeTableViewController.h"

#import "CZApp.h"

@interface Hometableviewcontroller ()

Container for plist file data

@property (Nonatomic,strong) Nsarray *listarray;


Manage queues for global download operations

@property (Nonatomic,strong) Nsoperationqueue *opqueue;


Cache pool for all download operations

@property (Nonatomic,strong) nsmutabledictionary *operationcache;


Caching of all images

@property (Nonatomic,strong) nsmutabledictionary *imagecache;


@end


@implementation Hometableviewcontroller


Lazy load

-(Nsarray *) listarray{

if (_listarray ==nil) {

NSString *path = [[nsbundlemainbundle]pathforresource:@ ' videos.plist ' oftype:nil];

Nsarray *dictarray = [Nsarrayarraywithcontentsoffile:path];

Dictionary Turn model

Nsmutablearray *muarray = [[Nsmutablearrayalloc]init];

For (nsdictionary *dictin Dictarray) {

Czapp *app = [Czappappwithdict:dict];

[Muarray Addobject:app];

}

_listarray = Muarray;

}

Return_listarray;

}


-(Nsmutabledictionary *) operationcache{

if (_operationcache ==nil) {

_operationcache = [Nsmutabledictionarydictionary];

}

Return_operationcache;

}


-(Nsmutabledictionary *) imagecache{

if (_imagecache ==nil) {

_imagecache = [[Nsmutabledictionaryalloc]init];

}

Return_imagecache;

}


-(Nsoperationqueue *) opqueue{

if (_opqueue ==nil) {

_opqueue = [[Nsoperationqueuealloc]init];

}

Return_opqueue;

}


-(void) Viewdidload {

[Superviewdidload];


}


#pragma mark-table View data source


-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {

RETURN1;

}


-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {

Returnself.listArray.count;

}


-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {

Hometableviewcell *cell=[tableviewdequeuereusablecellwithidentifier:@ "Cell"];

if (Cell==nil) {

cell=[[hometableviewcellalloc]initwithstyle:uitableviewcellstyledefaultreuseidentifier:@ "Cell"];

Cell.selectionstyle=uitableviewcellselectionstylenone;

}

Czapp *app =self.listarray[indexpath.row];

Cell.label.text = App.video_title;

Cell.detailLabel.text = App.video_subtitle;

Determine if there are pictures in the picture cache pool

if ([self.imageCacheobjectForKey:app.video_img]) {

NSLog (@ "Do not download pictures on the Internet, from the image buffer pool);"

Cell.imageView.image =self.imagecache[app.video_img];

return cell;

}

Show placeholder Pictures

Cell.dimageView.image = [uiimageimagenamed:@ "User_default"];

Download picture (refactoring cut out)

[Selfdownloadimage:indexpath];

return cell;

}


-(void) Downloadimage: (Nsindexpath *) indexpath{

Czapp *app =self.listarray[indexpath.row];

To determine if there is an action in the Operation cache pool for downloading pictures

if (Self.operationcache[app.video_img]) {

NSLog (@ "Desperate to download from the cache pool ...");

Return

}

Multithreading asynchronous

Nsblockoperation *DOWNLOADOP = [nsblockoperationblockoperationwithblock:^{

NSLog (@ "Picture download ...");

1. Download pictures

NSData *data = [nsdatadatawithcontentsofurl:[nsurlurlwithstring:app.video_img]];

UIImage *image = [Uiimageimagewithdata:data];

2. Put the downloaded picture in the Imagecache cache

if (image) {

[Self.imageCachesetObject:image forKey:app.video_img];

To remove an action from the cache pool

[Self.operationCacheremoveObjectForKey:app.video_img];

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.