Mar 3, 2014

Roating CGImageRef on iOS.

For reminder to myself, rotate image on center.

    CGImageRef imageref = [self imageFromSampleBuffer:sampleBuffer];
    // or any other CGImageRef source.
    
    int w = CGImageGetWidth(imageref);
    int h = CGImageGetHeight(imageref);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageref);
    CGContextRef bitmap = CGBitmapContextCreate(NULL, w, h,
                                                CGImageGetBitsPerComponent(imageref), 
                                                CGImageGetBytesPerRow(imageref),
                                                colorSpaceInfo, CGImageGetBitmapInfo(imageref));
    CGContextTranslateCTM(bitmap, w/2, h/2);
    CGContextRotateCTM(bitmap, M_PI_4);
    CGContextTranslateCTM(bitmap,  -w/2, -h/2;
    CGContextDrawImage(bitmap, CGRectMake(0, 0, w, h), imageref);
    CGImageRelease(imageref);
    imageref = CGBitmapContextCreateImage(bitmap);
    CGContextRelease(bitmap);
    return imagereff;

No comments: