0

更新 1

我在这里找到了一个很有前途的苹果文档,描述了 CGDataProviderCopyData。我认为这通过从上下文中获取绘图并提取像素值来完成我最初询问的问题。

示例代码使用CGImageGetDataProvider了一些我不理解的其他功能,因此我无法弄清楚如何实现它们的功能。如何从变量con或其上下文中获取信息并访问像素?

更新 1

更新 0

也许我在这里问错了问题。CGContextDrawImage在我的情况下,将图像从 104 x 104 缩放到 13 x 13,然后CGContextDrawImage显示图像。也许我需要找到CGContextDrawImage仅进行缩放的部分。

initWithData:scale:在“UIImage 类参考”中找到了。但我不知道如何为该方法提供数据。我想要的比例是 0.25 。

- (id)initWithData:(NSData *)data scale:(CGFloat)scale

有人可以告诉我如何(NSData *)data为我的应用程序提供吗?

更新 0

//
//  BSViewController.m

#import "BSViewController.h"

@interface BSViewController ()
@end

@implementation BSViewController


- (IBAction) chooseImage:(id) sender{


    [self.view addSubview:self.imageView];
    UIImage* testCard = [UIImage imageNamed:@"ipad 7D.JPG"];
    CGSize sz = [testCard size];
    CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
    UIGraphicsBeginImageContext(CGSizeMake( 250,650));
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
    UIGraphicsEndImageContext();
    self.workingImage = CFBridgingRelease(num);
    CGImageRelease(num);

我正在研究从上到下的过渡。

更具体地说,我需要为 imageRef 提供正确的输入。我想给 imageRef 一个 13 x 13 的图像,但是当我给 imageRefnum它得到一个 104 x 104 的图像,而当我给 imageRefcon它得到一个 0 x 0 的图像。(底部提到了另一种尝试性的方法。)

下面的代码是 Brandon Trebitowski 的

    CGImageRef imageRef = num;
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    NSLog(@"the width: %u", width);
    NSLog(@"the height: %u", height);
    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);
    NSLog(@"Stop 3");

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;
    for (int ii = 0 ; ii < width * height ; ++ii)
    {
        int outputColor = (rawData[byteIndex] + rawData[byteIndex+1] + rawData[byteIndex+2]) / 3;

        rawData[byteIndex] = (char) (outputColor);
        rawData[byteIndex+1] =  (char) (outputColor);
        rawData[byteIndex+2] = (char) (outputColor);

        byteIndex += 4;
    }
}


- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我还尝试定义self.workingImage以下两种方式之一并将其提供给imageRef.

self.workingImage = num;
self.workingImage = (__bridge UIImage *)(num);
CGImageRef imageRef = [self.workingImage CGImage];
4

1 回答 1

0

我更改了 2 行并添加了 3 行并得到了我想要的结果。主要的变化是使用UIGraphicsBeginImageContextWithOptions代替,UIGraphicsBeginImageContext以便可以在绘制图像之前完成重新缩放。

@implementation BSViewController


- (IBAction) chooseImage:(id) sender{


    [self.view addSubview:self.imageView];
    UIImage* testCard = [UIImage imageNamed:@"ipad 7D.JPG"];
    CGSize sz = [testCard size];
    CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
    UIGraphicsBeginImageContextWithOptions(CGSizeMake( 104,104), NO, 0.125);  // Changed
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, CGRectMake(0, 0, 104, 104) ,num);  // Changed
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();  // Added
    UIGraphicsEndImageContext();
    self.workingImage = CFBridgingRelease(num);
    CGImageRelease(num);


    UIImageView* iv = [[UIImageView alloc] initWithImage:im];   // Added
    [self.imageView addSubview: iv];  // Added
    CGImageRef imageRef = [im CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);

等等

于 2013-01-25T20:02:03.547 回答