短视频直播源码,iOS图片去背景相关的代码
代码语言:javascript复制- (UIImage *)removeBackgroudWithImage: (UIImage *)image{
unsigned char *targetData = malloc(sizeof(unsigned char) * image.size.width *image.size.height *4);
UIGraphicsBeginImageContext(image.size);
[image drawInRect: CGRectMake(0, 0, image.size.width, image.size.height)];
unsigned char *data = malloc(sizeof(unsigned char) * image.size.width *image.size.height *4);
[self imageRGBABytesRef: &data FromImage: image];
for (int i = 0; i < image.size.width *image.size.height *4; i = 4) {
UIColor *pixleColor = [UIColor colorWithRed: data[i] / 255.0 green:data[i 1] / 255.0 blue:data[i 2] / 255.0 alpha:data[i 3] / 255.0];
CGFloat c, m, y ,k, a;
[pixleColor getCyan:&c magenta:&m yellow:&y black:&k alpha:&a];//YYKit中的类别方法
targetData[i] = data[i];
targetData[i 1] = data[i 1];
targetData[i 2] = data[i 2];
if (k < 0.4) {
targetData[i 3] = 0;
}else{
targetData[i 3] = 255;
}
}
UIImage *result = [self imageFromBRGABytes: targetData imageSize: image.size];
free(targetData);
return result;
}
- (UIImage *)imageFromBRGABytes:(unsigned char *)imageBytes imageSize:(CGSize)imageSize {
CGImageRef imageRef = [self imageRefFromBGRABytes:imageBytes imageSize:imageSize];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return image;
}
- (CGImageRef)imageRefFromBGRABytes:(unsigned char *)imageBytes imageSize:(CGSize)imageSize {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(imageBytes,
imageSize.width,
imageSize.height,
8,
imageSize.width * 4,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return imageRef;
}
- (void)imageRGBABytesRef:(unsigned char **)data FromImage: (UIImage *)image {
unsigned char *targetData = malloc(sizeof(unsigned char) * image.size.width *image.size.height *4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(targetData,
image.size.width,
image.size.height,
8,
image.size.width * 4,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
memcpy(*data, targetData, sizeof(unsigned char) * image.size.width *image.size.height *4);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
}
以上就是短视频直播源码,iOS图片去背景相关的代码, 更多内容欢迎关注之后的文章