zoukankan      html  css  js  c++  java
  • iOS之CATiledLayer的属性简介和使用

    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
  • 相关阅读:
    CentOS 8.0配置阿里云yum源和epel源
    CentOS8 安装epel 使用阿里云镜像
    centos下yum使用proxy代理方法
    MySQL中的事务控制(一)start transaction
    MySQL中的锁定语句: lock tables 和 unlock tables
    MySQL中的触发器
    MySQL中的事件调度器
    MySQL中的流程控制
    MySQL中的不可见索引、倒序索引
    IDEA出现Push to origin/master was rejected
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/7773246.html
Copyright © 2011-2022 走看看