2012-02-05 2 views
2

지금 당장 Quartz 2D를 사용하여 프로그래밍 방식으로 이미지를 생성 할 수 있습니다. 저는 그랜드 센트럴 디스패치와 함께 사용하기를 원했기 때문에 다른 CPU에서 생성되어 완성되었을 때 일반적인 페이드를 유발할 수 있습니다. 지금은이 게시물의 하단에있는 코드를 사용하지만 잘못된 문맥 오류가 발생합니다. 이것을 할 수있는 방법이 있습니까 아니면 운이 좋습니까?Quartz 2D 및 Grand 프로그래밍 방식으로 UIImage를 생성하는 방법

CGContextTranslateCTM : 유효하지 않은 컨텍스트 0x0으로 CGContextScaleCTM : 유효하지 않은 컨텍스트 0x0으로 CGContextSaveGState : 유효하지 않은 컨텍스트 0x0으로 CGContextSetCompositeOperation : 유효하지 않은 컨텍스트 0x0으로 CGContextFillRects : 유효하지 않은 컨텍스트 0x0으로 CGContextRestoreGState : 유효하지 않은 컨텍스트 0x0으로 CGContextDrawImage : 잘못된 컨텍스트 0x0으로

내 코드 :

dispatch_group_t group = dispatch_group_create(); 
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ 
     self.view.contentScaleFactor=[[UIScreen mainScreen] scale]; 
     CGSize sizeofText=[stopName sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:17.5*[self.view contentScaleFactor]]]; 
     CGSize size; 
     size.width=1024; 
     size.height=1024; 

     UIImage *leftCap = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" ofType:@"png"]]; 
     UIImage *center = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" ofType:@"png"]]; 
     UIImage *rightCap = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" ofType:@"png"]]; 
     UIImage *spike = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[email protected]" ofType:@"png"]]; 
     UIGraphicsBeginImageContextWithOptions(size,false,0); 

     CGContextRef context = UIGraphicsGetCurrentContext(); //get the context we just made above 
     CGContextTranslateCTM(context, 0,size.height); 
     CGContextScaleCTM(context, 1, -1); 

     width=(19*[self.view contentScaleFactor])+(sizeofText.width)+(43*[self.view contentScaleFactor]); 

     height=60*[self.view contentScaleFactor]; 

     //draw calls 
     [leftCap drawInRect:CGRectMake(0,1024- 54.0f*[self.view contentScaleFactor], 19.0f*[self.view contentScaleFactor], 54.0f*[self.view contentScaleFactor])];  
     [center drawInRect:CGRectMake(19.0f*[self.view contentScaleFactor],1024- 54.0f*[self.view contentScaleFactor],(sizeofText.width), 54.0f*[self.view contentScaleFactor])]; 
     [rightCap drawInRect:CGRectMake((sizeofText.width)+19*[self.view contentScaleFactor],1024- 54.0f*[self.view contentScaleFactor], 43.0f*[self.view contentScaleFactor], 54.0f*[self.view contentScaleFactor])]; 
     [spike drawInRect:CGRectMake((width/2)-((32.0f*[self.view contentScaleFactor])/2.0f),1024-(43.5*[self.view contentScaleFactor])- 27.0f*[self.view contentScaleFactor], 32.0f*[self.view contentScaleFactor], 27.0f*[self.view contentScaleFactor])]; 

     CGContextSelectFont(context, "Helvetica-Bold", 17.5*[self.view contentScaleFactor], kCGEncodingMacRoman); 
     CGContextSetTextDrawingMode(context, kCGTextFill); 
     CGContextSetTextPosition(context,19.f*[self.view contentScaleFactor],1024- (7.5*[self.view contentScaleFactor])-(sizeofText.height)); 
     CGContextShowText(context, [stopName UTF8String], strlen([stopName UTF8String])); 

     image=UIGraphicsGetImageFromCurrentImageContext(); 



     UIGraphicsEndImageContext(); 

    }); 



    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ 
     NSLog(@"done. I would trigger the fade in animation ehre"); 
    }); 

답변

4

나는 UIGraphicsBeginImageContextWithOptions 대신 CGBitmapContextCreate를 사용하고 있다는 점을 제외하고는 비슷한 것을합니다. 백그라운드 스레드에서 UI * 함수를 호출하기 때문에 컨텍스트가 작성되지 않은 것처럼 보입니다. 오유 마음을 해달라고하면

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 

CGContextRef _composedImageContext = CGBitmapContextCreate(NULL, 
               width, 
               height, 
               8, 
               width*4, 
               rgbColorSpace, 
               kCGImageAlphaPremultipliedFirst); 

CGColorSpaceRelease(rgbColorSpace); 


// draw your things into _composedImageContext 

//finally turn the context into a CGImage 
CGImageRef cgImage = CGBitmapContextCreateImage(_composedImageContext); 
+0

은 정확하게 당신이 당신의 문맥을 창조 하셨 는가 내가 어떻게 UIGraphicsBeginImageContextwithOptions은과 같이 주 스레드에서 만들 수 있지만 여전히 작동하지 않는 이동 : http://pastebin.com/JZi7esx7 – jfisk

+0

나는했습니다 지금 내 대답에 코드 샘플을 추가했습니다. –

관련 문제