Bri*_*tow 3 cocoa bitmap objective-c nsimage cgimage
好吧,看来我正在创建一个PDFDocument,其中pixelWidth在我创建的图像中不正确.所以问题就变成了:如何在图像中获得正确的分辨率?
我从扫描仪的位图数据开始.我这样做:
CGDataProviderRef provider= CGDataProviderCreateWithData(NULL (UInt8*)data, bytesPerRow * length, NULL);
CGImageRef cgImg = CGImageCreate (
width,
length,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorspace,
bitmapinfo, // ? CGBitmapInfo bitmapInfo,
provider, //? CGDataProviderRef provider,
NULL, //const CGFloat decode[],
true, //bool shouldInterpolate,
kCGRenderingIntentDefault // CGColorRenderingIntent intent
);
/* CGColorSpaceRelease(colorspace); */
NSData* imgData = [NSMutableData data];
CGImageDestinationRef dest = CGImageDestinationCreateWithData
(imgData, kUTTypeTIFF, 1, NULL);
CGImageDestinationAddImage(dest, cgImg, NULL);
CGImageDestinationFinalize(dest);
NSImage* img = [[NSImage alloc] initWithData: imgData];
Run Code Online (Sandbox Code Playgroud)
似乎没有在任何地方在那里,包括以英寸或点的实际宽度/高度,也没有实际的分辨率,这是我不要知道在这一点......我怎么做到这一点?
如果你有一大块数据,最简单的方法NSImage就是使用它NSBitmapImageRep.特别是:
NSData * byteData = [NSData dataWithBytes:data length:length];
NSBitmapImageRep * imageRep = [NSBitmapImageRep imageRepWithData:byteData];
NSSize imageSize = NSMakeSize(CGImageGetWidth([imageRep CGImage]), CGImageGetHeight([imageRep CGImage]));
NSImage * image = [[NSImage alloc] initWithSize:imageSize];
[image addRepresentation:imageRep];
...use image
Run Code Online (Sandbox Code Playgroud)
小智 7
希望这会有所帮助
size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
provider, // data provider
NULL, // decode
YES, // should interpolate
renderingIntent);
_image = [[NSImage alloc] initWithCGImage:iref size:NSMakeSize(width, height)];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8653 次 |
| 最近记录: |