【问题标题】:mask text inside uitextview/uiwebview屏蔽 uitextview/uiwebview 中的文本
【发布时间】:2009-11-24 10:04:29
【问题描述】:

最后我选择花一些时间来寻找一种方法/实现 屏蔽 UITextView/UIWebView 中的文本。 现在我能做的是: - 添加一些自定义背景 - 添加带有一些文本的 uitextview/uiwebview - 添加一个 UIImageView(带有覆盖 png)或一个 CAGradientLayer 到 创建一个简单的蒙版效果 (*) 当然这不是灵丹妙药,至少还需要一颗 层(用 * 指出的那一层)。 此外,当你有一个完全透明的时候它不是那么好 背景 '因为每个人都可以识别出额外的视图/图层 淡出文字。 我搜索了整个谷歌,但仍然没有找到一个好的解决方案(我已经 发现关于屏蔽图像,等等等等)...... 有小费吗? 提前致谢, 马西奥

PS 也许截图会更直接,给你! http://grab.by/KzS

【问题讨论】:

    标签: iphone objective-c uikit


    【解决方案1】:

    是的!我终于明白了。我不知道这是否是苹果的方式,但它确实有效。也许他们有机会使用一些私有 api。无论如何,这是一种关于我如何工作的伪算法:

    1) 获取窗口截图

    2) 使用 CGImageCreateWithImageInRect 裁剪所需的矩形

    3) 应用渐变蒙版(从 Apple 的 Reflections 示例代码中窃取)

    4) 使用新创建的图像创建一个 UIImageView

    我还注意到,即使在最低的设备上,它也不会影响性能。 希望它会有所帮助! 这是结果的一部分 (link text)

    我已经向自己承诺实施一个类别只是为了让它变得更好。到目前为止,代码在不同的类中相当分散。 只是为了制作一个示例(仅支持横向,请参见下面的转换,仅支持顶部蒙版)。在这种情况下,我覆盖了需要屏蔽的表的 didMoveToWindow:

    - (void)didMoveToWindow {
        if (self.window) {
    
            UIImageView *reflected = (UIImageView *)[self.superview viewWithTag:TABLE_SHADOW_TOP];
            if (!reflected) {
                UIImage *image = [UIImage screenshot:self.window];
    
                //
                CGRect croppedRect = CGRectMake(480-self.frame.size.height, self.frame.origin.x, 16, self.frame.size.width);
                CGImageRef cropImage = CGImageCreateWithImageInRect(image.CGImage, croppedRect);
                UIImage *reflectedImage = [UIImage imageMaskedWithGradient:cropImage];
                CGImageRelease(cropImage);
    
                UIImageView *reflected = [[UIImageView alloc] initWithImage:reflectedImage];
                reflected.transform = CGAffineTransformMakeRotation(-(M_PI/2));
                reflected.tag = TABLE_SHADOW_TOP;
                CGRect adjusted = reflected.frame;
                adjusted.origin = self.frame.origin;
                reflected.frame = adjusted;
                [self.superview addSubview:reflected];
                [reflected release];
            }
        }
    }
    

    这是 uiimage 类别:

    CGImageRef CreateGradientImage(int pixelsWide, int pixelsHigh)
    {
        CGImageRef theCGImage = NULL;
    
        // gradient is always black-white and the mask must be in the gray colorspace
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    
        // create the bitmap context
        CGContextRef gradientBitmapContext = CGBitmapContextCreate(NULL, pixelsWide, pixelsHigh,
                                                                   8, 0, colorSpace, kCGImageAlphaNone);
    
        // define the start and end grayscale values (with the alpha, even though
        // our bitmap context doesn't support alpha the gradient requires it)
        CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};
    
        // create the CGGradient and then release the gray color space
        CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
        CGColorSpaceRelease(colorSpace);
    
        // create the start and end points for the gradient vector (straight down)
        CGPoint gradientStartPoint = CGPointZero;
        //  CGPoint gradientStartPoint = CGPointMake(0, pixelsHigh);
        CGPoint gradientEndPoint = CGPointMake(pixelsWide/1.75, 0);
    
        // draw the gradient into the gray bitmap context
        CGContextDrawLinearGradient(gradientBitmapContext, grayScaleGradient, gradientStartPoint,
                                    gradientEndPoint, kCGGradientDrawsAfterEndLocation);
        CGGradientRelease(grayScaleGradient);
    
        // convert the context into a CGImageRef and release the context
        theCGImage = CGBitmapContextCreateImage(gradientBitmapContext);
        CGContextRelease(gradientBitmapContext);
    
        // return the imageref containing the gradient
        return theCGImage;
    }
    
    CGContextRef MyCreateBitmapContext(int pixelsWide, int pixelsHigh)
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        // create the bitmap context
        CGContextRef bitmapContext = CGBitmapContextCreate (NULL, pixelsWide, pixelsHigh, 8,
                                                            0, colorSpace,
                                                            // this will give us an optimal BGRA format for the device:
                                                            (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
        CGColorSpaceRelease(colorSpace);
    
        return bitmapContext;
    }
    
    + (UIImage *)imageMaskedWithGradient:(CGImageRef)image {
    
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        DEBUG(@"need to support deviceOrientation: %i", deviceOrientation);
    
        float width  = CGImageGetWidth(image);
        float height = CGImageGetHeight(image);
    
        // create a bitmap graphics context the size of the image
        CGContextRef mainViewContentContext = MyCreateBitmapContext(width, height);
    
        // create a 2 bit CGImage containing a gradient that will be used for masking the 
        // main view content to create the 'fade' of the reflection.  The CGImageCreateWithMask
        // function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient
        CGImageRef gradientMaskImage = CreateGradientImage(width, 1);
    
        // create an image by masking the bitmap of the mainView content with the gradient view
        // then release the  pre-masked content bitmap and the gradient bitmap
        CGContextClipToMask(mainViewContentContext, CGRectMake(0.0, 0.0, width, height), gradientMaskImage);
        CGImageRelease(gradientMaskImage);
    
        // draw the image into the bitmap context
        CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, width, height), image);
    
        // create CGImageRef of the main view bitmap content, and then release that bitmap context
        CGImageRef reflectionImage = CGBitmapContextCreateImage(mainViewContentContext);
        CGContextRelease(mainViewContentContext);
    
        // convert the finished reflection image to a UIImage 
        UIImage *theImage = [UIImage imageWithCGImage:reflectionImage];
    
        // image is retained by the property setting above, so we can release the original
        CGImageRelease(reflectionImage);
    
        return theImage;
    
    }
    

    希望对你有帮助。

    【讨论】:

    • 感谢分享。我目前正在尝试做一些与您稍有不同的事情——裁剪 UITextView 以便在其后面显示另一个 UITextView ...参见here。您是否知道如何裁剪 UITextView?如果您有任何建议,我将不胜感激!
    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 2011-07-24
    • 2012-06-27
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2021-10-10
    相关资源
    最近更新 更多