【问题标题】:CGBitmapContextCreateImage or CGContextDrawImage doesn't workCGBitmapContextCreateImage 或 CGContextDrawImage 不起作用
【发布时间】:2014-06-22 17:00:53
【问题描述】:

我无法解决这个问题。加载图片,转换为CGImageRef。尝试获取位图上下文并在屏幕上渲染。

 NSURL *imageFileURL = [NSURL fileURLWithPath:stringIMG];

   CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
   CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);

    NSInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
    NSInteger bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    NSInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
    NSInteger width = CGImageGetWidth(imageRef);
    NSInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorspace = CGImageGetColorSpace(imageRef);

    size_t rawData = bytesPerRow*height;
    unsigned char *data = malloc(rawData);
    memset(data2, 0, rawData);

     CGContextRef context = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorspace, bitmapInfo);


    CGImageRef imageRef2 = CGBitmapContextCreateImage(context);

   // CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef2);
    UIImage *result = [UIImage imageWithCGImage:imageRef2];//if i do like this i got white empty screen

    UIImageView *image = [[UIImageView alloc] initWithImage:result];
    [self.view addSubview:image];// if i do like this i got black rectangle on the white screen

我不知道。我通过断点检查上下文不为空。我不知道我该怎么办。请问有人可以帮我吗?

【问题讨论】:

  • 你在主线程吗?
  • 我想是的。我没有做另一个线程。怎么查?
  • 你应该用一个:CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);但是,更简单的是,为什么不直接使用 imageRef 而不是 imageRef2 创建呢?

标签: objective-c xcode uiimage cgcontext cgimageref


【解决方案1】:

Category 将其添加到 UIImage 以使用 CoreGraphics 调整大小

UIImage+Resize.h

#import <UIKit/UIKit.h>

@interface UIImage (Resizing)

-(UIImage*)resizedImageWithSize:(CGSize)size;

@end

UIImage+Resizing.m

#import "UIImage+Resizing.h"

@implementation UIImage (Resizing)

-(UIImage*)resizedImageWithSize:(CGSize)size {


    CGImageRef cgImage = [self CGImage];

    size_t bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
    size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgImage);

    CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);

    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), cgImage);

    CGImageRef resizedImageRef = CGBitmapContextCreateImage(context);

    UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef];

    CFRelease(resizedImageRef);
    CFRelease(context);

    return resizedImage;

}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多