拍下UIView的美麗容顏

  1. 產生容納合成畫面的baseView
 UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];

2. 在baseView上加上blueView和yellowView

UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  100, 100)];
blueView.backgroundColor = [UIColor blueColor];
[baseView addSubview:blueView];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 100, 100)];
yellowView.backgroundColor = [UIColor yellowColor];
[baseView addSubview:yellowView];

3. 從baseView生成合成圖片image

UIGraphicsBeginImageContext(baseView.bounds.size);
[baseView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

4. 將圖片顯示在imageView上

UIImageView *imageView = [[UIImageView alloc]
initWithFrame:CGRectMake(20, 20, 200, 100)];
imageView.image = image ;
[self.view addSubview:imageView];

結果

Email me when 彼得潘的App Neverland publishes or recommends stories