gpt4 book ai didi

ios - 屏蔽 UIImage iOS 8

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:44 27 4
gpt4 key购买 nike

我使用以下代码在 iOS 7 中屏蔽 uiimages,效果很好。但是现在,在 iOS 8 中它什么都不做,它没有返回原始图像 + 蒙版,而是返回了一个黑色图像。

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef imgRef = [image CGImage];
CGImageRef maskRef = [maskImage CGImage];
CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
return [UIImage imageWithCGImage:masked];

这是我的应用程序的工作原理:- 原始图像将变得模糊- 然后应用蒙版(圆形、方形、任何形状。这是 .png 图像)- 返回模糊图像,mask 应该裁剪模糊图像并看到原始图像后面没有模糊。它适用于 iOS 7,但在 iOS 8 中,有关屏蔽的代码(以上)不起作用。有什么想法吗?

最佳答案

使用此方法在 iOS 8.0 中屏蔽图像。我也在我的运行代码中使用了这种方法并且工作正常。

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef imgRef = [image CGImage];
CGImageRef maskRef = [maskImage CGImage];


int maskWidth = CGImageGetWidth(maskRef);
int maskHeight = CGImageGetHeight(maskRef);
// round bytesPerRow to the nearest 16 bytes, for performance's sake
int bytesPerRow = (maskWidth + 15) & 0xfffffff0;
int bufferSize = bytesPerRow * maskHeight;

// allocate memory for the bits
CFMutableDataRef dataBuffer = CFDataCreateMutable(kCFAllocatorDefault, 0);
CFDataSetLength(dataBuffer, bufferSize);

// the data will be 8 bits per pixel, no alpha
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceGray();
CGContextRef ctx = CGBitmapContextCreate(CFDataGetMutableBytePtr(dataBuffer),
maskWidth, maskHeight,
8, bytesPerRow, colourSpace, kCGImageAlphaNone);
// drawing into this context will draw into the dataBuffer.
CGContextDrawImage(ctx, CGRectMake(0, 0, maskWidth, maskHeight), maskRef);
CGContextRelease(ctx);

// now make a mask from the data.
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(dataBuffer);
CGImageRef mask = CGImageMaskCreate(maskWidth, maskHeight, 8, 8, bytesPerRow,
dataProvider, NULL, FALSE);

CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colourSpace);
CFRelease(dataBuffer);

CGImageRef masked = CGImageCreateWithMask(imgRef, mask);
return [UIImage imageWithCGImage:masked];

}

关于ios - 屏蔽 UIImage iOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com