12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- //
- // CPDFDrawImageView.m
- // PDFViewer
- //
- // Created by kdanmobile_2 on 2023/1/3.
- //
- #import "PDFBackgroundDrawImageView.h"
- @implementation PDFBackgroundDrawImageView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
-
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- }
-
- return self;
- }
- - (void)drawRect:(CGRect)rect {
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- [self showDraw:context];
- }
- - (void)showDraw:(CGContextRef)context {
- UIImage *newImage = [self imageToTransparent:_image];
-
- [newImage drawInRect:self.bounds blendMode:kCGBlendModeNormal alpha:1.0f];
- }
- - (UIImage*) imageToTransparent:(UIImage*) image {
- const int imageWidth = image.size.width;
- const int imageHeight = image.size.height;
- size_t bytesPerRow = imageWidth * 4;
- uint32_t *rgbImageBuf = (uint32_t *)malloc(bytesPerRow * imageHeight);
-
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace
- , kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
-
- CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
-
- int pixelNum = imageWidth * imageHeight;
- uint32_t* pCurPtr = rgbImageBuf;
- for (int i = 0; i < pixelNum; i++, pCurPtr++)
- {
- if ((*pCurPtr & 0x65815A00) == 0x65815a00)
- {
- uint8_t* ptr = (uint8_t*)pCurPtr;
- ptr[0] = 0;
- }
- }
- CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
- CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace,
- kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider,
- NULL, true,kCGRenderingIntentDefault);
- CGDataProviderRelease(dataProvider);
- UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- CGContextRelease(context);
- CGColorSpaceRelease(colorSpace);
- return resultUIImage;
- }
- void ProviderReleaseData(void *info, const void *data, size_t size) {
- free((void *) data);
- }
- @end
|