무엇이 누락 되었습니까? 몇 시간 동안 붙어있어 drawRect가 예상대로 그려지며 getTheImage()가 viewcontoller에서 호출됩니다. UIGraphicsGetImageFromCurrentImageContext()는 nil을 반환합니다. 많은 변형을 시도했다. 도움을 주시면 감사하겠습니다.IOS Quartz2D가 현재 컨텍스트에서 이미지를 가져올 수 없습니다.
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContext(self.bounds.size);
CGContextSetLineWidth(context, 5.0);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor]CGColor]);
CGContextMoveToPoint(context,100.0,100.0);
CGContextAddLineToPoint(context,200.0,200.0);
CGContextMoveToPoint(context,200.0,200.0);
CGContextAddLineToPoint(context,200.0,400.0);
CGContextStrokePath(context);
}
-(UIImage *)getTheImage
{
UIImage *drawImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"drawImage %@",drawImage);
return drawImage;
}
답장을 보내 주셔서 감사합니다. 여전히 작동하지 않습니다. UIGraphicsBeginImageContext는 1 개의 인수 만 사용합니다. –
UIGraphicsBeginImageContext를 UIGraphicsBeginImageContextWithOptions으로 변경했습니다. 계속 진행하십시오. –
UIGraphicsBeginImageContextWithOptions에 대해 죄송합니다. 이 getTheImage 메소드를 어디에서 호출하는지 말해 줄 수 있습니까? 이 메서드는 뷰 컨트롤러의 viewWillAppear 및 viewDidAppear 메서드에서 호출되었으며 두 위치에서 모두 이미지가 나오지 않습니다. 그러나 viewDidLoad에서이 메서드를 호출하면 이미지가 0이됩니다. 뷰가 완전히로드 된 후에 만 getTheImage 메서드를 호출해야합니다. –