2011-12-14 15 views
1

私の前の質問Crop and Scale CMSampleBufferRefの後に、そのCMSampleBufferRefの中のCGContextを修正する方法を見つけました。次のコードを使って、CGContextの四角形、パス、円、およびクリアの矩形を描くことができました。 :CGContextを切り抜くCGContext - iOS

- (void)modifyImage:(CMSampleBufferRef) sampleBuffer { 
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    // Lock the image buffer 
    CVPixelBufferLockBaseAddress(imageBuffer,0); 

    // Get information about the image 
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a CGImageRef from the CVImageBufferRef 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

    CGContextSaveGState(context); 

    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); 
    CGContextFillRect(context, CGRectMake(0, 0, 400, 400)); 

    //restore the context and remove the clipping area. 
    CGContextRestoreGState(context); 


    // We unlock the image buffer 
    CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

    // We release some components 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    return; 
} 

は今、私がやりたいことはトリミングしてから、このCGContextをスケーリングすることで、私はCGContextClipToRect(context, CGRectMake(0, 0, 360, 640));CGContextScaleCTM(context, 2, 2);ではなく、成功してみました。

は、誰もがここに

答えて

0

あなたが作成後CVImageBufferの寸法を変更することはできません私にいくつかのより多くのアドバイスを与えることができます。ディメンションを変更する必要がある場合は、新しいバッファを作成する必要があります。

クロッピングされた部分を元のイメージのサイズに拡大するには、2番目のバッファにスケーリング操作を実行し、次に2番目のバッファから最初のバッファにコピーする必要があります。

+0

私はCVContextRefを変更し、その新しいCVContextRefで新しいCMSampleBufferRefを再作成することを意味しますか? – vodkhang

+0

こんにちは、あなたの提案をどうすればいいですか?あなたはサンプルコードを持っていますか、私はしばらくの間研究した後、これが本当に必要です – vodkhang