Which CGImageAlphaInfo should we use?
Confirmed by an Apple engineer at WWDC 2014 that we should use kCGImageAlphaPremultipliedFirst
or kCGImageAlphaNoneSkipFirst
, and that it does affect performance.
Bitwise operations with CGBitmapInfo and CGImageAlphaInfo
You have the right equivalent Swift code:
bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)
It's a little strange because CGImageAlphaInfo
isn't actually a bitmask -- it's just a UInt32 enum
(or a CF_ENUM/NS_ENUM with type uint32_t
, in C parlance), with values for 0 through 7.
What's actually happening is that your first line clears the first five bits of bitmapInfo
, which is a bitmask (aka RawOptionSetType
in Swift), since CGBitmapInfo.AlphaInfoMask
is 31, or 0b11111. Then your second line sticks the raw value of the CGImageAlphaInfo
enum into those cleared bits.
I haven't seen enums and bitmasks combined like this anywhere else, if that explains why there isn't really documentation. Since CGImageAlphaInfo
is an enum, its values are mutually exclusive. This wouldn't make any sense:
bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)
bitmapInfo |= CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
CGBitmapInfo alpha mask after swift 2.0
error:Cannot convert value of type 'CGBitmapInfo' to expected argument of type UInt32
Swift 2.0 actually expects a UInt32 instead of a CGBitMapInfo object, so you should take out a UInt32 Variable in CGBitMapInfo.
CGBitmapContextCreate(
nil,
Int(ceil(pixelSize.width)),
Int(ceil(pixelSize.height)),
CGImageGetBitsPerComponent(originalImageRef),
0,
colorSpace,
bitmapInfo.rawValue)
https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGBitmapContext/#//apple_ref/c/func/CGBitmapContextCreate
iPhone: Changing CGImageAlphaInfo of CGImage
Yeah, I had problems with 8 bit (indexed) .PNGs. I had to convert it to a more native image to perform graphics operations. I essentially did something like this:
- (UIImage *) normalize {
CGColorSpaceRef genericColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef thumbBitmapCtxt = CGBitmapContextCreate(NULL,
self.size.width,
self.size.height,
8, (4 * self.size.width),
genericColorSpace,
kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(genericColorSpace);
CGContextSetInterpolationQuality(thumbBitmapCtxt, kCGInterpolationDefault);
CGRect destRect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextDrawImage(thumbBitmapCtxt, destRect, self.CGImage);
CGImageRef tmpThumbImage = CGBitmapContextCreateImage(thumbBitmapCtxt);
CGContextRelease(thumbBitmapCtxt);
UIImage *result = [UIImage imageWithCGImage:tmpThumbImage];
CGImageRelease(tmpThumbImage);
return result;
}
Combining CGBitmapInfo and CGImageAlphaInfo in Swift
You can make it a little simpler:
let bimapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
.union(.ByteOrder32Little)
You unfortunately can't get away from converting the CGImageAlphaInfo
into a CGBitmapInfo
. That's just a weakness in the current API. But once you have it, you can use .union
to combine it with other values. And once the enum type is known, you don't have to keep repeating it.
It's weird to me that there's no operator available here. I've opened a radar for that, and included an |
implementation. http://www.openradar.me/23516367
public func |<T: SetAlgebraType>(lhs: T, rhs: T) -> T {
return lhs.union(rhs)
}
@warn_unused_result
public func |=<T : SetAlgebraType>(inout lhs: T, rhs: T) {
lhs.unionInPlace(rhs)
}
let bimapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
| .ByteOrder32Little
how should i be considering CGImageAlphaInfo when converting to JPEG?
It doesn't much matter, because JPEG doesn't support alpha (at least, not without external masking or an extension), so you can expect that CGImageDestination will throw away the alpha channel at the export stage.
I would try the original image's alpha info first, and if I can't create the bitmap context that way, I would use either kCGImageAlphaNoneSkipFirst
or kCGImageAlphaNoneSkipLast
. For other destination file formats, of course, I'd use one of the alpha-with-premultiplied-colors pixel formats.
This page has the full list of combinations supported by CGBitmapContext.
how should i be considering CGImageAlphaInfo when converting to JPEG?
It doesn't much matter, because JPEG doesn't support alpha (at least, not without external masking or an extension), so you can expect that CGImageDestination will throw away the alpha channel at the export stage.
I would try the original image's alpha info first, and if I can't create the bitmap context that way, I would use either kCGImageAlphaNoneSkipFirst
or kCGImageAlphaNoneSkipLast
. For other destination file formats, of course, I'd use one of the alpha-with-premultiplied-colors pixel formats.
This page has the full list of combinations supported by CGBitmapContext.
Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapinfo' (aka) 'enum CGBitmapInfo') ios 7
Syntax for CGBitmapContextCreate
CGContextRef CGBitmapContextCreate (
void *data,
size_t width,
size_t height,
size_t bitsPerComponent,
size_t bytesPerRow,
CGColorSpaceRef colorspace,
CGBitmapInfo bitmapInfo
);
See the last argument which belong to CGBitmapInfo
. But you're using kCGImageAlphaPremultipliedLast
which belong to CGImageAlphaInfo
. So just cast type to CGBitmapInfo
kCGImageAlphaNone unresolved identifier in swift
You have to create a struct CGBitmapInfo
from the CGImageAlphaInfo.None
value:
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.None.rawValue)
var context = CGBitmapContextCreate(nil, UInt(width), UInt(height), 8, 0, colorSpace, bitmapInfo)
Related Topics
Disable Warning Dialog If Bluetooth Is Powered Off iOS
Module 'Googlemobileads' Not Found in iOS
Remove Uiwebview's Internal Cache
iOS Uiwebview Crash in "Webthread"
Google 400 Error: Invalid Request Custom Scheme Uris Are Not Allowed for 'Web' Client Type
How to Create Multi Line Uisegmentedcontrol
Download PDF and Save to the "Files" in Iphone, Not to the App Data, Swift
How to Save Custom Objects in Array and Store It in Nsuserdefaults - Iphone
Open a Wkwebview Target="_Blank" Link in Safari
Avassetresourceloaderdelegate Methods Not Working on Device
Insert String at Cursor Position of Uitextfield
Physicsbody Doesn't Adhere to Node's Anchor Point
Downcast from Any to a Protocol
How to Default Uilabel Font and Size Using Swift
None of Your Accounts Are a Member, Code Signing Errors After Upgrading to Xcode 8
Admob 7.3.1 and Swift 2.0 - Module Not Found
Swift, Dispatch_Group_Wait Not Waiting
How to Have Firebase Automatically Delete Values Older Than 30 Minutes