代码之家  ›  专栏  ›  技术社区  ›  marcio

uitextview/uiwebview中的掩码文本

  •  1
  • marcio  · 技术社区  · 16 年前

    最后,我选择花一些时间来找到一种方法/实现 在UITextView/UIWebView中屏蔽文本。 到目前为止,我能做的是: -添加一些自定义背景 -添加带有一些文本的uitextview/uiwebview -将UIImageView(带有覆盖png)或CAGradientLayer添加到 创建简单的遮罩效果(*) 当然,这不是一个神奇的子弹,至少还需要一个 层(用*指出的层)。 此外,当你有一个完全透明的 背景,因为每个人都可以识别用于 淡出文字。 我在谷歌上到处搜索,但仍然没有找到一个好的解决方案(我已经 找到了关于遮罩的图片,等等)。。。 有什么提示吗? 提前感谢, 马西奥

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

    1 回复  |  直到 16 年前
        1
  •  1
  •   marcio    15 年前

    对我终于明白了。我不知道这是否是苹果的方式,但它是有效的。也许他们有机会使用一些私有API。不管怎样,这是一种伪算法,它是如何工作的:

    1) 获取窗口的屏幕截图

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

    3) 应用渐变遮罩(摘自Apple反射示例代码)

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

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

    只需制作一个示例(仅支持横向,请参阅下面的变换,仅支持顶部遮罩)。在本例中,我覆盖了需要屏蔽的表的didmovetown:

    - (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;
    
    }
    

    希望有帮助。