Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active December 7, 2018 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/9b6367e48a338e9b92c3 to your computer and use it in GitHub Desktop.
Save baobao/9b6367e48a338e9b92c3 to your computer and use it in GitHub Desktop.
UIImageViewとCALayerを切り替えるコード
//
// ViewController.m
// CALayerTest
//
// Created by Ohba Shunsuke on 2013/03/03.
// Copyright (c) 2013年 Ohba Shunsuke. All rights reserved.
//
#import "ViewController.h"
//CALayerを使用する際は、以下のクラスをインポートする必要がある
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *img = [UIImage imageNamed:@"bg"];
//CALayer生成
CALayer *l = [CALayer layer];
//CALayerに画像を貼り付ける(CGImageの参照を渡すだけ)
l.contents = (id)img.CGImage;
//コンテンツのスケールを設定(0~1)
l.contentsRect = CGRectMake(0, 0, 1, 1);
//CALayerのサイズを指定する
l.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
//中心が基準点になるため、座標を補正する
l.position = CGPointMake(img.size.width/2, img.size.height/2);
//UIImageView生成
UIImageView *view = [[UIImageView alloc] initWithImage:img];
//UIImageViewを表示
[self.view addSubview:view];
//CALayerを表示する
[self.view.layer addSublayer:l];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment