iOS之CATiledLayer的屬性簡介和使用

來源:互聯網
上載者:User

標籤: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_BEGINCA_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;@endNS_ASSUME_NONNULL_END

 

iOS之CATiledLayer的屬性簡介和使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.