Graphics Reference
In-Depth Information
forIndexPath
:indexPath];
//add the tiled layer
CATiledLayer
*tileLayer = [cell.
contentView
.
layer
.
sublayers
lastObject
];
if
(!tileLayer)
{
tileLayer = [
CATiledLayer
layer
];
tileLayer.
frame
= cell.
bounds
;
tileLayer.
contentsScale
= [
UIScreen
mainScreen
].
scale
;
tileLayer.
tileSize
=
CGSizeMake
(
cell.
bounds
.
size
.
width
* [
UIScreen
mainScreen
].
scale
,
cell.
bounds
.
size
.
height
* [
UIScreen
mainScreen
].
scale
);
tileLayer.
delegate
=
self
;
[tileLayer
setValue
:
@(
indexPath.
row
)
forKey
:
@"index"
];
[cell.
contentView
.
layer
addSublayer
:tileLayer];
}
//tag the layer with the correct index and reload
tileLayer.
contents
= nil;
[tileLayer
setValue
:
@(
indexPath.
row
)
forKey
:
@"index"
];
[tileLayer
setNeedsDisplay
];
return
cell;
}
- (
void
)drawLayer:(
CATiledLayer
*)layer inContext:(
CGContextRef
)ctx
{
//get image index
NSInteger
index = [[layer
valueForKey
:
@"index"
]
integerValue
];
//load tile image
NSString
*imagePath =
self
.
imagePaths
[index];
UIImage
*tileImage = [
UIImage
imageWithContentsOfFile
:imagePath];
//calculate image rect
CGFloat
aspectRatio = tileImage.
size
.
height
/ tileImage.
size
.
width
;
CGRect
imageRect =
CGRectZero
;
imageRect.
size
.
width
= layer.
bounds
.
size
.
width
;
imageRect.
size
.
height
= layer.
bounds
.
size
.
height
* aspectRatio;
imageRect.
origin
.
y
= (layer.
bounds
.
size
.
height
- imageRect.
size
.
height
)/
2
;
//draw tile
UIGraphicsPushContext
(ctx);
[tileImage
drawInRect
:imageRect];
UIGraphicsPopContext
();