知識點:iOS/Mac去掉截圖Alpha通道

在項目開發過程中,遇到一種很奇怪的現象:同一張圖片,在iOS、Mac端能很正常的顯示出來,但在H5、Android端卻顯示不出來。經過對比正常的圖片與這張異常圖片,發現區別在於異常圖片Alpha通道一項的值爲“是”,而正常能夠顯示出來的圖片該值爲“否”。查看圖片簡介如下:

那麼怎麼去掉一張截圖的Alpha通道呢?

1. 方法一

-(void)converter:(NSString*)name{
    
    NSLog(@"NAME: %@",name);

    NSURL *url = [NSURL fileURLWithPath:name];
    CGImageSourceRef source;
    NSImage *srcImage =[[NSImage alloc] initWithContentsOfURL:url];;
    NSLog(@"URL: %@",url);
    source = CGImageSourceCreateWithData((__bridge CFDataRef)[srcImage TIFFRepresentation], NULL);
    CGImageRef imageRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);
    CGRect rect = CGRectMake(0.f, 0.f, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       rect.size.width,
                                                       rect.size.height,
                                                       CGImageGetBitsPerComponent(imageRef),
                                                       CGImageGetBytesPerRow(imageRef),
                                                       CGImageGetColorSpace(imageRef),
                                                       kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Little
                                                       );
    
    CGContextDrawImage(bitmapContext, rect, imageRef);
    
    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext);
    
    NSImage *finalImage = [[NSImage alloc] initWithCGImage:decompressedImageRef size:NSZeroSize];
    NSData *imageData = [finalImage  TIFFRepresentation];
    NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
    imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
    [imageData writeToFile:name atomically:NO];
    
    CGImageRelease(decompressedImageRef);
    CGContextRelease(bitmapContext);
}

2. 方法二

CGImageRef imageRef = [image CGImageForProposedRect:&((CGRect){0,0,image.size.width,image.size.height}) context:nil hints:nil];
NSInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
NSInteger bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSInteger bitsPerSample = 16;
bool hasAlpha = NO;
NSInteger samplesPerPixel = hasAlpha ? 4 : 3;
///畫布
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes:NULL
                             pixelsWide:image.size.width
                             pixelsHigh:image.size.height
                             bitsPerSample:bitsPerSample
                             samplesPerPixel:samplesPerPixel
                             hasAlpha:NO
                             isPlanar:NO
                             colorSpaceName:NSCalibratedRGBColorSpace
                             bytesPerRow:bytesPerRow*(bitsPerSample/8)
                             bitsPerPixel:bitsPerPixel*(bitsPerSample/8)];
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];
    
[NSGraphicsContext setCurrentContext:context];
[image drawInRect:NSMakeRect(0, 0, image.size.width, image.size.height)  fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0];
NSData *data = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
NSImage *resultImage = [[NSImage alloc]initWithData:data];

方法不一樣,思路一致,可以根據自己的需求調整參數。輸出結果後,再次打開圖片簡介查看Alpha通道值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章