2013-01-02 216 views
1

我需要將圖像旋轉90度,即交換寬度和高度(翻轉圖像)。在將圖像提供給編碼器以創建視頻之前,需要完成此操作。我用下面的代碼(參考:How do I export UIImage array as a movie?CGAffineTransform旋轉90度

- (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image 
{ 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
     [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, 
     [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, 
     nil]; 
    CVPixelBufferRef pxbuffer = NULL; 
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, CGImageGetWidth(image), 
     CGImageGetHeight(image), kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, 
     &pxbuffer); 
    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL); 

    CVPixelBufferLockBaseAddress(pxbuffer, 0); 
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); 
    NSParameterAssert(pxdata != NULL); 

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pxdata, CGImageGetWidth(image), 
     CGImageGetHeight(image), 8, 4*CGImageGetWidth(image), rgbColorSpace, 
     kCGImageAlphaNoneSkipFirst); 
    NSParameterAssert(context); 
    CGContextConcatCTM(context, CGAffineTransformMakeRotation(1.571)); // ROTATION 
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
     CGImageGetHeight(image)), image); 
    CGColorSpaceRelease(rgbColorSpace); 
    CGContextRelease(context); 

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0); 

    return pxbuffer; 
} 

然而,通過90度的旋轉不工作(通過評論表示)。你能找出問題嗎?

回答

3

您應用的轉換是旋轉上下文,問題在於原點位於角落而不是上下文的中心。通過旋轉90度的圖像,你完全移動到負邊界,因此看不到任何東西。 假設在您的代碼中frameSize是旋轉圖像的大小(具有倒置的寬度/高度),您需要將該原點移動到中心並在其中應用旋轉。請嘗試:

CGContextTranslateCTM(context, frameSize.width/2, frameSize.height/2); 
CGContextRotateCTM(context, M_PI_2); 
CGContextTranslateCTM(context, -frameSize.height/2, -frameSize.width/2); 
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image); 


- (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image 
{ 
    CGSize frameSize = CGSizeMake(CGImageGetHeight(image), CGImageGetWidth(image)); 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
     [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, 
     [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, 
     nil]; 
    CVPixelBufferRef pxbuffer = NULL; 
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width, 
     frameSize.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, 
     &pxbuffer); 
    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL); 

    CVPixelBufferLockBaseAddress(pxbuffer, 0); 
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); 
    NSParameterAssert(pxdata != NULL); 

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width, 
     frameSize.height, 8, 4 * frameSize.width, rgbColorSpace, 
     kCGImageAlphaNoneSkipFirst); 
    NSParameterAssert(context); 
    CGContextTranslateCTM(context, frameSize.width/2, frameSize.height/2); 
    CGContextRotateCTM(context, M_PI_2); 
    CGContextTranslateCTM(context, -frameSize.height/2, -frameSize.width/2); 
    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image); 

    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
     CGImageGetHeight(image)), image); 
    CGColorSpaceRelease(rgbColorSpace); 
    CGContextRelease(context); 

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0); 

    return pxbuffer; 
} 
+0

感謝您的回覆。我明白你的意思,但該方法仍然無效。此外,frameSize本身就是圖像大小。對不起,我已經更新了代碼。 –

+1

這是我使用的代碼: CGContextTranslateCTM(context,CGImageGetHeight(image)/ 2,CGImageGetWidth(image)/ 2); CGContextRotateCTM(context,M_PI_2); CGContextTranslateCTM(context,-CGImageGetWidth(image)/ 2,-CGImageGetHeight(image)/ 2); 圖像旋轉但在旋轉時切出一半。 –

+0

添加完整的代碼。一探究竟。創建上下文時,您必須已經混淆了大小。 –