gpt4 book ai didi

iphone - iOS 如何有效地对 cgimage 执行旋转(最多 125 帧)?

转载 作者:可可西里 更新时间:2023-11-01 06:12:19 24 4
gpt4 key购买 nike

我有两张图片,我们称它们为图片 A 和图片 B。我正在尝试旋转和缩放图片 A 并将其绘制在图片 B 之上的特定位置。

我使用下面的函数和方法来: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;

}

最佳答案

如果您同时创建大量图像,请重复使用相同的 CGContext。另外,不要创建新的旋转图像;在现有上下文中旋转图像。

基本上,无论您最终使用它制作了多少图像,您都应该只需创建一次 CGContext。这应该会显着加快速度。

关于iphone - iOS 如何有效地对 cgimage 执行旋转(最多 125 帧)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8554943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com