view is now centered and rendering is flipped correctly

This commit is contained in:
Diego Waxemberg
2014-01-18 21:39:29 -05:00
parent f97fe7fce2
commit 72ed023645
3 changed files with 28 additions and 43 deletions
+8 -22
View File
@@ -27,17 +27,16 @@ static bool firstFrame = true;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"initWithFrame");
// Initialization code
width = 1280;
height = 720;
bitsPerComponent = 8;
bytesPerRow = (bitsPerComponent / 8) * width * 4;
pixelData = malloc(width * height * 4);
colorSpace = CGColorSpaceCreateDeviceRGB();
//bitmapContext = CGBitmapContextCreate(pixelData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault);
//image = CGBitmapContextCreateImage(bitmapContext);
return self;
}
@@ -47,34 +46,21 @@ static bool firstFrame = true;
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawRect");
if (!nv_avc_get_rgb_frame(pixelData, width*height*4))
{
NSLog(@"failed to decode frame!");
return;
}
if (firstFrame) {
NSData *data = [[NSData alloc] initWithBytes:pixelData length:(width*height*4)];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"];
[data writeToFile:appFile atomically:YES];
NSLog(@"writing data to: %@",documentsDirectory);
firstFrame = false;
}
bitmapContext = CGBitmapContextCreate(pixelData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
image = CGBitmapContextCreateImage(bitmapContext);
;
struct CGContext* context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, rect.size.width);
CGContextScaleCTM(context, (float)width / self.frame.size.width, (float)height / -self.frame.size.height);
CGContextRotateCTM(context, -M_PI_2);
CGContextScaleCTM(context, -(float)self.frame.size.width/self.frame.size.height, (float)self.frame.size.height/self.frame.size.width);
CGContextDrawImage(context, rect, image);
CGImageRelease(image);