Graphics Reference
In-Depth Information
▪
CATiledLayer
always requires us to redraw our image into a
CGContext
, even if it is
already the same size as our tile and has already been decompressed (and could
therefore be used directly as the layer contents, without redrawing).
Let's find out if those potential disadvantages make a difference in practice: Listing 14.4
shows a reimplementation of our image carousel using
CATiledLayer
.
Listing 14.4
The Image Carousel Updated to Use
CATiledLayer
for Loading
#import
"ViewController.h"
#import
<QuartzCore/QuartzCore.h>
@interface
ViewController
() <
UICollectionViewDataSource
>
@property
(
nonatomic
,
copy
)
NSArray
*imagePaths;
@property
(
nonatomic
,
weak
)
IBOutlet
UICollectionView
*collectionView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
//set up data
self
.
imagePaths
=
[[
NSBundle
mainBundle
]
pathsForResourcesOfType
:
@"jpg"
inDirectory
:
@"Vacation Photos"
];
//register cell class
[
self
.
collectionView
registerClass
:[
UICollectionViewCell
class
]
forCellWithReuseIdentifier
:
@"Cell"
];
}
- (
NSInteger
)collectionView:(
UICollectionView
*)collectionView
numberOfItemsInSection:(
NSInteger
)section
{
return
[
self
.
imagePaths
count
];
}
- (
UICollectionViewCell
*)collectionView:(
UICollectionView
*)collectionView
cellForItemAtIndexPath:(
NSIndexPath
*)indexPath
{
//dequeue cell
UICollectionViewCell
*cell =
[collectionView
dequeueReusableCellWithReuseIdentifier
:
@"Cell"