• 获取图片像素信息


    - (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count
    {
        NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity: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 = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
        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.
        NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
        for (int i = 0 ; i < count ; ++i)
        {
            CGFloat alpha = ((CGFloat) rawData[byteIndex + 3] ) / 255.0f;
            CGFloat red   = ((CGFloat) rawData[byteIndex]     ) / alpha;
            CGFloat green = ((CGFloat) rawData[byteIndex + 1] ) / alpha;
            CGFloat blue  = ((CGFloat) rawData[byteIndex + 2] ) / alpha;
            byteIndex += bytesPerPixel;
            
            UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
            
            [result addObject:acolor];
        }
        free(rawData);
        
        return result;
    }

    http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics

    __block NSString *leftStr = @"FirstLeft";

        

        NSString *centrStr = @"FirstCenter";

        

        NSString* (^myBlock)(NSString *) = ^(NSString *lastStr){

            

            return [leftStr stringByAppendingString:[NSString stringWithFormat:@", %@, %@", centrStr, lastStr]];

        };

        

        NSLog(@"%@", myBlock(@"last"));

        

        leftStr = @"ThenLeft";

        centrStr = @"ThenCenter";

        

        NSLog(@"%@", myBlock(@"right"));

  • 相关阅读:
    TKStudio示例工程LPC1220_GPIO_LED学习
    LIve Writer图片自动水印,自动居中,自动为原始大小的设置方法.
    cmd 修改文件属性
    [原创]Java下X86机,Bytes和Int的转换
    [原创]把","号分隔的字串转化成一列的Table
    [原创]Java实现PKCS7填充的DES加密(修订版)
    利用ADODB.Stream实现 Bytes到String的指定编码的转换
    [原创]利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆(修改)
    [原创]用XMLHttp BinaryWrite,Post GB2312编码的字串
    UTF8ToBytes
  • 原文地址:https://www.cnblogs.com/wly314/p/5354532.html
Copyright © 2020-2023  润新知