图像缩放CGDataProviderCopyData

时间:2013-01-20 18:58:25

标签: ios xcode image-processing

更新1

I have found a promising apple doc here描述CGDataProviderCopyData。我认为这可以通过从上下文中获取绘图并提取像素值来完成我最初提出的问题。

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

更新1

更新0

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

我在“UIImage类参考”中找到了initWithData:scale:。但我不知道如何提供该方法的数据。我想要的比例是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乘13的图像,但是当我给imageRef num时,它得到一个104乘104的图像,当我给imageRef con时,它得到一个0乘0的图像。 (底部提到了另一种尝试性方法。)

The code below is Brandon Trebitowski's

    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];

1 个答案:

答案 0 :(得分: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);