码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之CATiledLayer的属性简介和使用

时间:2017-11-02 18:20:14      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:ima   calayer   ceil   mcu   取图   分割   evel   apt   create   

1、CATiledLayer简介  

  CATiledLayer用于大型图片进行分割显示,需要显示的图片才会加载,直接上代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self cutImageAndSave];
    [self addTiledLayer];
    
}

- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)ctx{
    CGRect bounds = CGContextGetClipBoundingBox(ctx);
    NSInteger x = floor(bounds.origin.x / layer.tileSize.width);
    NSInteger y = floor(bounds.origin.y / layer.tileSize.height);
    
    //load tile image
    NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *imageName = [NSString stringWithFormat:@"%@/pic-%02ld-%02ld.png",filePath,x,y];
    UIImage *tileImage = [UIImage imageWithContentsOfFile:imageName];
    
    UIGraphicsPushContext(ctx);
    [tileImage drawInRect:bounds];
    UIGraphicsPopContext();
}

//添加CATiledLayer
- (void)addTiledLayer{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CScreenWidth, CScreenHeight)];
    [self.view addSubview:scrollView];
    
    UIImage *image = [UIImage imageNamed:@"pic1.jpg"];
    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(200, 200);
    _tiledLayer = tiledLayer;
    
    scrollView.contentSize = tiledLayer.frame.size;
    [scrollView.layer addSublayer:tiledLayer];
    [tiledLayer setNeedsDisplay];
}


//切图保存到沙盒
- (void)cutImageAndSave{
    NSString *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *imageName = [NSString stringWithFormat:@"%@/pic-00-00.png",filePath];
    UIImage *tileImage = [UIImage imageWithContentsOfFile:imageName];
    NSLog(@"%@",imageName);
    if (tileImage) return;
    
    UIImage *image = [UIImage imageNamed:@"pic1.jpg"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    CGFloat WH = 200;
    CGSize size = image.size;
    NSInteger rows = ceil(size.height / WH);
    NSInteger cols = ceil(size.width / WH);
    
    for (NSInteger y = 0; y < rows; ++y) {
        for (NSInteger x = 0; x < cols; ++x) {
            UIImage *subImage = [self captureView:imageView frame:CGRectMake(x*WH, y*WH, WH, WH)];
            NSString *path = [NSString stringWithFormat:@"%@/pic-%02ld-%02ld.png",filePath,x,y];
            [UIImagePNGRepresentation(subImage) writeToFile:path atomically:YES];
        }
    }
}

//切图
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)fra{
    //开启图形上下文 将heView的所有内容渲染到图形上下文中
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    
    //获取图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, fra);
    UIImage *i = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    return i;
}

效果图

技术分享

 

2、CATiledLayer属性

#import <QuartzCore/CALayer.h>

NS_ASSUME_NONNULL_BEGIN

CA_CLASS_AVAILABLE (10.5, 2.0, 9.0, 2.0)
@interface CATiledLayer : CALayer

//初次加载淡入时间,默认0.25s
//由于是类方法,无法直接修改,创建子类进行方法覆盖就行。
+ (CFTimeInterval)fadeDuration;


//这两个属性用处不太懂???
/* The number of levels of detail maintained by this layer. Defaults to * one. Each LOD is half the resolution of the previous level. If too * many levels are specified for the current size of the layer, then * the number of levels is clamped to the maximum value (the bottom * most LOD must contain at least a single pixel in each dimension). */ @property size_t levelsOfDetail; /* The number of magnified levels of detail for this layer. Defaults to * zero. Each previous level of detail is twice the resolution of the * later. E.g. specifying ‘levelsOfDetailBias‘ of two means that the * layer devotes two of its specified levels of detail to * magnification, i.e. 2x and 4x. */ @property size_t levelsOfDetailBias; //Defaults to (256, 256),设置CATiledLayer的item的大小 @property CGSize tileSize; @end NS_ASSUME_NONNULL_END

 

iOS之CATiledLayer的属性简介和使用

标签:ima   calayer   ceil   mcu   取图   分割   evel   apt   create   

原文地址:http://www.cnblogs.com/xianfeng-zhang/p/7773246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!