이미지를 합치는 작업을 하던 중 하나로 합쳐야하는 상황에서 일일히 크롭 붙이기를 반복하려다보니 문제가 많다는걸 느꼈다.
그냥 보이는 화면을 캡쳐하면 될거 같아 찾아보니 역시 있었다.
- (UIImage*)capture:(CGSize) size {
UIGraphicsBeginImageContextWithOptions(size, 1, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
UIGraphicsBeginImageContextWithOptions(size, 1, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
거의 크롭하는 것과 비슷한 구조인데 drawInRect를 쓰는대신 renderInContext를 사용하였다.
하지만! 이렇게 하다보니 저장할수 있는 사이즈는 화면에 보이는 사이즈 그대로일 뿐이다 (320x480) !
그래서 어떻게 할까 고민을 하다가 생각이 난 것이 UIScrollView 이다.
UIView에 원하는 사이즈만큼 View를 만든 다음 이미지를 얹은 후
ScrollView를 사용하여 화면에는 축소된 상태로 출력하니 생각 처럼 원하는 사이즈의 이미지로 저장할수 있었다.
@interface NCLayoutUIView : UIScrollView <UIScrollViewDelegate> {
............
}
......
- (void) makeLayoutView {
CGRect layoutFrame = self.bounds;
layoutFrame.size.width *= 2; // 실제 저장될 사이즈는 화면에 보이는 사이즈 x2 크기이다.
layoutFrame.size.height *= 2;
UIView * layoutView = [[UIView alloc]initWithFrame:layoutFrame];
[self addSubview:layoutView];
self.layoutView = layoutView;
self.layoutView.backgroundColor = COLOR_CLEAR;
self.layoutView.layer.borderWidth = 4.0; // 원래 테두리 굵기는 2px 이다.
self.layoutView.layer.borderColor = [COLOR_BLACK CGColor];
[layoutView release];
self.delegate = self;
self.maximumZoomScale = 0.5; // 이미지 사이즈 x2 크기이므로 화면에 보여질때는 2배 축소되서 보여져야한다.
self.minimumZoomScale = 0.5;
self.zoomScale = 0.5;
self.scrollEnabled = NO;
self.bouncesZoom = NO;
}
............
}
......
- (void) makeLayoutView {
CGRect layoutFrame = self.bounds;
layoutFrame.size.width *= 2; // 실제 저장될 사이즈는 화면에 보이는 사이즈 x2 크기이다.
layoutFrame.size.height *= 2;
UIView * layoutView = [[UIView alloc]initWithFrame:layoutFrame];
[self addSubview:layoutView];
self.layoutView = layoutView;
self.layoutView.backgroundColor = COLOR_CLEAR;
self.layoutView.layer.borderWidth = 4.0; // 원래 테두리 굵기는 2px 이다.
self.layoutView.layer.borderColor = [COLOR_BLACK CGColor];
[layoutView release];
self.delegate = self;
self.maximumZoomScale = 0.5; // 이미지 사이즈 x2 크기이므로 화면에 보여질때는 2배 축소되서 보여져야한다.
self.minimumZoomScale = 0.5;
self.zoomScale = 0.5;
self.scrollEnabled = NO;
self.bouncesZoom = NO;
}
하지만 단점은 원래 비율의 1/2크기로 출력되기 때문에 CALayer등으로 그린 선이 만약 0.5단위로 떨어지게 된다면 화면에 제대로 출력이 안되는 단점이 있다. 이를 방지하기 위해 되도록 모든 수치는 짝수로 떨어져야 하며 선 굵기등도 약 2배의 굵기로 그려줘여한다.
'iOS' 카테고리의 다른 글
유용한 오픈소스 링크 (0) | 2011.12.21 |
---|---|
ActionSheet 버튼 스타일 변경작업 (0) | 2011.12.16 |
메모리경고 발생시 (didReceiveMemoryWarning) Modal 창의 Parent 뷰 다운 현상 방지 (0) | 2011.11.02 |
UIView 와 Category의 만남 (0) | 2011.10.26 |
UIButton Title 위치 정하기 (0) | 2011.10.26 |