【问题标题】:Landscape images work, portrait images don't横向图像有效,纵向图像无效
【发布时间】:2010-10-18 08:06:17
【问题描述】:

我是使用 Objective C 编程的新手。我尝试读取 argb 数据 从图像中,然后显示该图像。它适用于横向图像,但不适用于纵向图像。这是我使用的代码。

//Read the data
CGImageRef inImage = dispimage.CGImage;
CGColorSpaceRef colorspace = CGImageGetColorSpace(inImage);
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];

if (cgctx == NULL) { return nil; /* error */ }

size_t width = CGImageGetWidth(inImage);
size_t height = CGImageGetHeight(inImage);

CGRect rect = {{0,0},{width,height}}; 
CGContextDrawImage(cgctx, rect, inImage); 

unsigned char* data = CGBitmapContextGetData (cgctx);

unsigned char alpha;
unsigned char R  [height][width];
unsigned char G [height][width];
unsigned char B [height][width];    

CGContextRelease(cgctx);

int length=4*width*height;
unsigned char result[length];
int i,j;
int kk=0;
for (i=0; i<height; i++) {
    for (j=0; j<width; j++) {
        result[kk]=alpha;
        result[kk+1]=R[i][j]; 
        result[kk+2]=G[i][j]; 
        result[kk+3]=B[i][j];
        kk=kk+4;
    }

}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL) {
    NSLog(@"Error allocating color space\n");
    return NULL;
}

CGContextRef context =CGBitmapContextCreate(result, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedFirst);


CGColorSpaceRelease(colorSpace);

CGImageRef imageRef = CGBitmapContextCreateImage (context);

UIImage *rawImage = [[UIImage imageWithCGImage:imageRef]retain];

imageView.image=rawImage;

输出的肖像图像甚至没有一种正确的颜色。

【问题讨论】:

    标签: objective-c iphone xcode uiimageview cgcontext


    【解决方案1】:

    只要保持天气条件是横向模式或纵向模式,然后更改 imageView 框架

    imageView.image=rawImage; 在这里,您要添加到 imageview,然后根据需要在两种情况下更改 imageview 的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      相关资源
      最近更新 更多