All pastes #834748 Raw Edit

Anonym

public text v1 · immutable
#834748 ·published 2007-12-28 12:59 UTC
rendered paste body
NSData * rotateImage(UIImage *picture)
{
	NSMutableData *output = [[[NSMutableData alloc] init] autorelease];
	CGImageRef imageRef = [picture imageRef];
	
	struct CGSize imgSize = [picture size];
	float _width = imgSize.width;
	float _height = imgSize.height;

	CGContextRef bitmap = CGBitmapContextCreate(
		NULL,
		_height,
		_width,
		CGImageGetBitsPerComponent(imageRef),
		4*_height,
		CGImageGetColorSpace(imageRef),
		CGImageGetBitmapInfo(imageRef)
	);
	
	CGContextTranslateCTM( bitmap, 0, _height );
	CGContextRotateCTM( bitmap, -90*(M_PI/180) );
	CGContextDrawImage( bitmap, CGRectMake(0,0,_height,_width), imageRef );
	CGImageRef ref = CGBitmapContextCreateImage( bitmap );
	
	CGImageDestinationRef dest = CGImageDestinationCreateWithData( (CFMutableDataRef)output, CFSTR("public.jpeg"), 1, NULL );
	CGImageDestinationAddImage(dest, ref, NULL);
	CGImageDestinationFinalize(dest);
	
	CGContextRelease( bitmap );
	CGImageRelease( ref );
	
	return [NSData dataWithData: output];
}