2012-02-05 8 views
2

今は、Quartz 2Dを使ってプログラムでタップでイメージを生成しています。私はグランドセントラルディスパッチと組み合わせて使用​​したかったので、別のCPUで作成し、完成時に通常のフェードをトリガすることができます。今私はこのポストの最後にある以下のコードを使用しますが、これらの無効なコンテキストエラーを取得しています。これを行う方法はありますか、私は幸運のうちにいますか?Quartz 2DとGrand CEntral Dispatchを使用して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

を私はしました私の答えにコードサンプルを追加しました –

関連する問題