2017-02-07 4 views
0

무엇이 누락 되었습니까? 몇 시간 동안 붙어있어 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; 
} 

답변

0

는이 함수를 호출해야합니다. 현재 컨텍스트가 nil이거나 UIGraphicsBeginImageContext에 대한 호출로 만들어지지 않은 경우이 함수는 nil을 반환합니다.

외관상으로는 귀하의 전화가 상황에 부합하지 않습니다.

0

시도해 볼 수 있습니까? 비트 맵 기반 그래픽 문맥 현재 그래픽 컨텍스트 경우에만

-(UIImage *)getTheImage 
{ 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale); 
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; 
    UIImage *drawImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    NSLog(@"drawImage %@",drawImage); 
    return drawImage; 
} 
+0

답장을 보내 주셔서 감사합니다. 여전히 작동하지 않습니다. UIGraphicsBeginImageContext는 1 개의 인수 만 사용합니다. –

+0

UIGraphicsBeginImageContext를 UIGraphicsBeginImageContextWithOptions으로 변경했습니다. 계속 진행하십시오. –

+0

UIGraphicsBeginImageContextWithOptions에 대해 죄송합니다. 이 getTheImage 메소드를 어디에서 호출하는지 말해 줄 수 있습니까? 이 메서드는 뷰 컨트롤러의 viewWillAppear 및 viewDidAppear 메서드에서 호출되었으며 두 위치에서 모두 이미지가 나오지 않습니다. 그러나 viewDidLoad에서이 메서드를 호출하면 이미지가 0이됩니다. 뷰가 완전히로드 된 후에 만 ​​getTheImage 메서드를 호출해야합니다. –

관련 문제