내 UIView 하위 클래스에 사각형을 그리는 중이고 줄의 너비가 두 배가되는 것을 제외하고는 모든 것이 작동합니다 (Retina 버전의 iPhone 시뮬레이터 atm 사용).).iPhone에서 코어 그래픽을 사용하여 사각형 그리기
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect innerRect = CGRectInset(rect, 5, 5);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextSetLineWidth(context, 2);
CGContextMoveToPoint(context, CGRectGetMinX(innerRect), CGRectGetMinY(innerRect));
CGContextAddLineToPoint(context, CGRectGetMaxX(innerRect), CGRectGetMinY(innerRect));
CGContextAddLineToPoint(context, CGRectGetMaxX(innerRect), CGRectGetMaxY(innerRect));
CGContextAddLineToPoint(context, CGRectGetMinX(innerRect), CGRectGetMaxY(innerRect));
CGContextClosePath(context);
CGContextStrokePath(context);
}
이 코드는 오히려 지정된 2 픽셀보다 올바른 위치에 있지만, 폭 4 픽셀로 사각형을 그립니다
이것은의 drawRect 방법입니다.
, 내가 스케일 계수를 설정 할 수 있습니다 참조 인터넷에 보면, 그래서 추가하는 시도 :
self.contentScaleFactor = [UIScreen mainScreen].scale;
self.layer.contentsScale = [UIScreen mainScreen].scale;
방법의 시작 부분에, 그 작동하지 않았다 때 나는 시도 :
self.contentScaleFactor = 2;
self.layer.contentsScale = 2;
(2 등은 망막 디스플레이에 대한 scale
방법에 의해 리턴 된 번호입니다) 그리고 그 중 하나가 작동하지 않았다.
시뮬레이터에 문제가 있습니까?이 문제는 기기에서 실행할 때 수정됩니까? (5.1로 업그레이드했지만 Xcode를 업데이트하는 것을 잊어 버렸습니다. 따라서 현재 Xcode 업데이트가 다운로드를 완료 할 때까지 2 시간이 걸립니다.)
또는 누락 된 것이 있습니까?
[CGContextStrokeRectWithWidth' 함수] (http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//)를 사용하여 많은 코드를 저장할 수 있습니다. apple_ref/c/func/CGContextStrokeRectWithWidth). –
@PeterHosey, 알아요, 실제로 둥근 사각형을 그리는 중입니다. 방금 원호 코드를 없애고 좌표를 단순화하여 질문을 읽기 쉽게 만듭니다. –