I'm trying to use AdobeLabsUXMagicSelectionView and I'm facing with 2 problems. I wanna "cut" the selected area (foreground) using these 2 methods:
Method 1) getForeground:andMatte:
It doesn't give me the correct foreground. When I select an area and call getForeground:andMatte
I gives me foreground and background (mixed).
Documentation says:
Alternatively, if you don’t need to process the underlying bitmap directly, and intend to use the results as inputs to CoreGraphics or CoreImage, you can call:
Method 2) After this I'm trying to "cut" as the documentation does
extension AdobeLabsUXMagicSelectionView {
func foregroundCGImage() -> CGImage {
let w = size_t(self.image.size.width)
let h = size_t(self.image.size.height)
let data = UnsafeMutablePointer<UInt8>(malloc(4 * w * h * sizeof(UInt8)))
self.readForegroundAndMatteIntoBuffer(data)
for var i = 0; i < 4 * w * h; i += 4 {
let alpha: UInt8 = UInt8(data[i + 3]) / 255
data[i] *= alpha
data[i + 1] *= alpha
data[i + 2] *= alpha
}
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipLast.rawValue)
let ctx = CGBitmapContextCreate(data, w, h, 8, 4 * w, CGColorSpaceCreateDeviceRGB(), bitmapInfo.rawValue)
let imageRef = CGBitmapContextCreateImage(ctx)!
return imageRef
}
}
But it only paints (black) the non-selected portion (background) of the image.
Anyone can help? What I want is get a final image of selected area.
UPDATE:
As @DonWoodward said I've create this Cattegory:
@implementation AdobeLabsUXMagicSelectionView (Foreground)
- (UIImage *)getForeground {
// show the results
// first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h
size_t w = self.image.size.width;
size_t h = self.image.size.height;
uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t));
[self readForegroundAndMatteIntoBuffer:data];
// Paint the non-selected portion of the image black
for (int i = 0; i < 4*w*h; i += 4) {
float alpha = (float)data[i + 3] / 255;
data[i ] *= alpha;
data[i + 1] *= alpha;
data[i + 2] *= alpha;
}
CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return foregroundBits;
}
@end
But the result has a lot of black pixel around the "foreground".
What I need? Get a "clean" foreground (selected area without black pixels) to put over an UIImageView