4
我有兩個圖像,我們稱它們爲圖像A和圖像B.我試圖旋轉和縮放圖像A,並在特定位置將其繪製在圖像B的頂部。iOS如何有效地執行cgimage的旋轉(最多125幀)?
我使用下面的功能和方法: 1.旋轉和縮放我的圖像 2.將圖像A繪製到圖像B的特定位置。
此代碼工作正常,但相當緩慢,有沒有辦法改善它?
功能旋轉和縮放:
CGImageRef rotateAndScaleImageCreate(const CGImageRef cgImage, const CGFloat radians,const CGFloat scalefactor){
CGImageRef rotatedImageRef = NULL;
const CGFloat originalWidth = CGImageGetWidth(cgImage)*scalefactor;
const CGFloat originalHeight = CGImageGetHeight(cgImage)*scalefactor;
const CGRect imgRect = (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = originalWidth, .size.height = originalHeight};
const CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, CGAffineTransformMakeRotation(radians));
/// Create an ARGB bitmap context
CGContextRef bmContext = NYXImageCreateARGBBitmapContext(rotatedRect.size.width, rotatedRect.size.height, 0);
if (!bmContext)
return nil;
/// Rotation happen here
CGContextTranslateCTM(bmContext, +(rotatedRect.size.width * 0.5f), +(rotatedRect.size.height * 0.5f));
CGContextRotateCTM(bmContext, radians);
/// Draw the image in the bitmap context
CGContextDrawImage(bmContext, (CGRect){.origin.x = -originalWidth * 0.5f, .origin.y = -originalHeight * 0.5f, .size.width = originalWidth, .size.height = originalHeight}, cgImage);
/// Create an image object from the context
rotatedImageRef = CGBitmapContextCreateImage(bmContext);
/// Cleanup
//CGImageRelease(rotatedImageRef);
CGContextRelease(bmContext);
return rotatedImageRef;
}
方法在特定地點的另一個圖像中繪製圖像:
-(UIImage*) PositionImage:(CGImageRef)image backgroundImage:(CGImageRef)backgroundImage atPointX:(float)X pointY:(float)Y withScale:(float)scale andRotation:(float)rotation{
//////////////////////////////////////////
CGLayerRef layer;
CGImageRef resultImage;
CGContextRef context, layerContext;
void *bitmapData;
CGColorSpaceRef colorSpace;
CGSize canvasSize;
int bitmapByteCount;
int bitmapBytesPerRow;
//Get the background image
//UIImage * backgroundImg = [UIImage imageNamed:@"Mask-Inverted.png"];
//Initialize the canvas size!
canvasSize = CGSizeMake(480,640);//[backgroundImg size];
//
bitmapBytesPerRow = (canvasSize.width * 4);
bitmapByteCount = (bitmapBytesPerRow * canvasSize.height);
//Create the color space
colorSpace = CGColorSpaceCreateDeviceRGB();
bitmapData = malloc(bitmapByteCount);
//Check the the buffer is alloc'd
if(bitmapData == NULL){
NSLog(@"Buffer could not be alloc'd");
}
//Create the context
context = CGBitmapContextCreate(bitmapData, canvasSize.width, canvasSize.height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
if(context == NULL){
NSLog(@"Context could not be created");
}
///Create the Layer
layer = CGLayerCreateWithContext(context, canvasSize, NULL);
if(layer == NULL){
NSLog(@"Layer could not be created");
}
///Create the layer Context
layerContext = CGLayerGetContext(layer);
if(layerContext == NULL){
NSLog(@"No Layer context");
}
//Draw the image background into the bitmap context
CGContextDrawImage(context, CGRectMake(0.0f,0.0f,480,640),backgroundImage);
//Rotate and Scale Image
CGImageRef rotatedImage =rotateAndScaleImageCreate(image,rotation,scale);
//Draw a image on the layer
CGContextDrawImage(layerContext,CGRectMake(0,0,CGImageGetWidth(rotatedImage),CGImageGetHeight(rotatedImage)),rotatedImage);
CGImageRelease(rotatedImage);
//Draw the layer in the context at the coordinate
CGContextDrawLayerAtPoint(context, CGPointMake(X-CGImageGetWidth(rotatedImage)*0.5f, Y-CGImageGetHeight(rotatedImage)*0.5f), layer);
CGLayerRelease(layer);
//Get the result image
resultImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
//Cleanup
free(bitmapData);
CGColorSpaceRelease(colorSpace);
//Create UIImage
UIImage *imageReturned = [UIImage imageWithCGImage:resultImage];
CGImageRelease(resultImage);
return imageReturned;
}
不要以爲你想要達到的速度(125 FPS)可以使用iphone。 – samfisher 2011-12-27 09:59:50
我有5個來處理它們,所以我的目標是25幀/秒。 – 2011-12-27 21:40:34
我一直在嘗試使用CIImage,但它泄漏http://stackoverflow.com/q/8643462/1097850 – 2011-12-28 16:51:28