【问题标题】:Making touched area of image transparent in Iphone在Iphone中使图像的触摸区域透明
【发布时间】:2011-04-07 09:19:10
【问题描述】:

我想让 UIImage 的触摸像素透明。 我看到了iPhone Objective C: How to get a pixel's color of the touched point on an UIImageView?

使用该代码,我可以定位图像触摸像素。但我不知道如何使该像素透明并更新 UIImage。

请帮助我。

【问题讨论】:

    标签: iphone ipad ios4


    【解决方案1】:

    希望对你有帮助

    What is the fastest way to draw single pixels directly to the screen in an iPhone application? 从这个 SO question How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

    + (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count
    {
        NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
    
        // First get the image into your data buffer
        CGImageRef imageRef = [image CGImage];
        NSUInteger width = CGImageGetWidth(imageRef);
        NSUInteger height = CGImageGetHeight(imageRef);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        unsigned char *rawData = malloc(height * width * 4);
        NSUInteger bytesPerPixel = 4;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        NSUInteger bitsPerComponent = 8;
        CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                        bitsPerComponent, bytesPerRow, colorSpace,
                        kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
    
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        CGContextRelease(context);
    
        // Now your rawData contains the image data in the RGBA8888 pixel format.
        int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
        for (int ii = 0 ; ii < count ; ++ii)
        {
            CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
            CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
            CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
            CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
            byteIndex += 4;
    
            UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
            [result addObject:acolor];
        }
    
      free(rawData);
    
      return result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 2011-07-16
      • 2013-06-26
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多