零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods
一.CVPixelBufferRef 转 UIImage
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:CVPixelBufferRef 转 UIImage
//@Time:2022/01/21 07:20
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
- (UIImage *)imageFromPixelBuffer:(CVPixelBufferRef)pixelBuffer {
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer];
CIContext *context = [CIContext new];
CGImageRef cgimage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage *image= [UIImage imageWithCGImage:cgimage];
CGImageRelease(cgimage);
return image;
}
二. UIImage 转 CVPixelBufferRef
注意:
kCVPixelFormatType_OneComponent8 是单通道的黑白数据;
kCVPixelFormatType_32ARGB 是带有颜色的 ARGB 数据;
kCVPixelFormatType_32BGRA 是带有颜色的 RGBA 数据;
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:UIImage 转 CVPixelBufferRef
//@Time:2022/01/21 07:20
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
static uint32_t bitmapInfoWithPixelFormatType(OSType inputPixelFormat, bool hasAlpha){
if (inputPixelFormat == kCVPixelFormatType_32BGRA) {
uint32_t bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
if (!hasAlpha) {
bitmapInfo = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host;
}
return bitmapInfo;
}else if (inputPixelFormat == kCVPixelFormatType_32ARGB) {
uint32_t bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big;
return bitmapInfo;
}else if (inputPixelFormat == kCVPixelFormatType_OneComponent8) {
uint32_t bitmapInfo = kCGImageAlphaNone;
return bitmapInfo;
}else{
NSLog(@"不支持此格式");
return 0;
}
}
// alpha的判断
BOOL IsCGImageRefContainsAlpha(CGImageRef imageRef) {
if (!imageRef) {
return NO;
}
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
BOOL hasAlpha = !(alphaInfo == kCGImageAlphaNone ||
alphaInfo == kCGImageAlphaNoneSkipFirst ||
alphaInfo == kCGImageAlphaNoneSkipLast);
return hasAlpha;
}
// 此方法能还原真实的图片
- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image pixelFormatType:(OSType)pixelFormatType resizeSize:(CGSize)resizeSize {
// CGSize size = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
BOOL hasAlpha = IsCGImageRefContainsAlpha(image);
CFDictionaryRef empty = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
empty, kCVPixelBufferIOSurfacePropertiesKey,
nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, resizeSize.width, resizeSize.height, pixelFormatType, (__bridge CFDictionaryRef) options, &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata;
if (pixelFormatType == kCVPixelFormatType_OneComponent8) {
pxdata = CVPixelBufferGetBaseAddressOfPlane(pxbuffer,0);
}else {
pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
}
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef colorSpace;
size_t bytesPerRow;
if (pixelFormatType == kCVPixelFormatType_OneComponent8) {
colorSpace = CGColorSpaceCreateDeviceGray();
bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pxbuffer, 0);
}else {
colorSpace = CGColorSpaceCreateDeviceRGB();
bytesPerRow = CVPixelBufferGetBytesPerRow(pxbuffer);
}
uint32_t bitmapInfo = bitmapInfoWithPixelFormatType(pixelFormatType, (bool)hasAlpha);
CGContextRef context = CGBitmapContextCreate(pxdata, resizeSize.width, resizeSize.height, 8, bytesPerRow, colorSpace, bitmapInfo);
NSParameterAssert(context);
CGContextDrawImage(context, CGRectMake(0, 0, resizeSize.width, resizeSize.height), image);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
return pxbuffer;
}
三.猜你喜欢
- iOS NSString 拆分字符串
- iOS NSString 截取字符串
- iOS NSString 和 NSArray 相互转换
- iOS NSString 包含字符串/匹配字符串
- iOS NSURL URLWithString 与 fileURLWithPath 区别
- iOS NSURL 与 NSString相互转换
- iOS NSString 获取中文字符串长度
- iOS NSString 判断字符串是否为空判
- iOS NSString 获取指定下标字符
- iOS NSString 匹配字符串开头
- iOS NSString 匹配字符串结尾
- iOS NSString 判断两个字符串是否相等
- iOS NSString 将字符串中的字母转换为大写
- iOS NSString 将字符串中的字母转换为小写
- iOS NSString 将字符串中的首字母变为大写
- iOS NSString 拼接字符串
- iOS NSString 插入字符串
- iOS NSString 删除字符串
- iOS NSNumber 和 int 相互转换
- iOS NSNumber compare 比较
- iOS NSNumber isEqualToNumber
- iOS NSValue 和 CGPoint 相互转换
- iOS NSValue 和 CGSize 相互转换
- iOS UIImage 与 RGBA 相互转换
- Object-C UIImage 缩放
- Object-C UIImage 裁剪
- Object-C UIImage 旋转
- Object-C UIImage 合并多张图片
- Object-C UIImage 黑白处理
- Object-C UIImage 美白
- Object-C UIImage 磨皮美白
- Object-C UIImage 添加文字
- Object-C UIImage 和 CVPixelBufferRef 相互转换
ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容