如何将NSImage转换为NSBitmapImageRep?我有密码:
- (NSBitmapImageRep *)bitmapImageRepresentation
{
NSBitmapImageRep *ret = (NSBitmapImageRep *)[self representations];
if(![ret isKindOfClass:[NSBitmapImageRep class]])
{
ret = nil;
for(NSBitmapImageRep *rep in [self representations])
if([rep isKindOfClass:[NSBitmapImageRep class]])
{
ret = rep;
break;
}
}
if(ret == nil)
{
NSSize size = [self size];
size_t width = size.width;
size_t height = size.height;
size_t bitsPerComp = 32;
size_t bytesPerPixel = (bitsPerComp / CHAR_BIT) * 4;
size_t bytesPerRow = bytesPerPixel * width;
size_t totalBytes = height * bytesPerRow;
NSMutableData *data = [NSMutableData dataWithBytesNoCopy:calloc(totalBytes, 1) length:totalBytes freeWhenDone:YES];
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef ctx = CGBitmapContextCreate([data mutableBytes], width, height, bitsPerComp, bytesPerRow, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), kCGBitmapFloatComponents | kCGImageAlphaPremultipliedLast);
if(ctx != NULL)
{
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:[self isFlipped]]];
[self drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
CGImageRef img = CGBitmapContextCreateImage(ctx);
ret = [[NSBitmapImageRep alloc] initWithCGImage:img];
[self addRepresentation:ret];
CFRelease(img);
CFRelease(space);
CGContextRelease(ctx);
}
}
return ret;
}
它可以工作,但它会导致内存泄漏。至少当我和ARC一起使用它的时候。使用initWithData:[nsimagename TIFFRepresentation]
,它不能正常工作。有些形象的表现不太好。我认为这取决于图像的格式和颜色空间。还有其他方法来实现这一点吗?
mrwalker的结果提出了解决方案:
原始图像:
转换为bitmapimagerep并返回到图像1次:
转换为bitmapimagerep并返回到图像3次:
正如您所看到的,每次转换到NSBitmapImageRep后,图像都会变暗。
发布于 2012-10-15 14:18:44
您可以尝试从Mike Ash's "Obtaining and Interpreting Image Data" blog post改编的这种方法。
- (NSBitmapImageRep *)bitmapImageRepresentation {
int width = [self size].width;
int height = [self size].height;
if(width < 1 || height < 1)
return nil;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: width
pixelsHigh: height
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: width * 4
bitsPerPixel: 32];
NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep: rep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: ctx];
[self drawAtPoint: NSZeroPoint fromRect: NSZeroRect operation: NSCompositeCopy fraction: 1.0];
[ctx flushGraphics];
[NSGraphicsContext restoreGraphicsState];
return [rep autorelease];
}
发布于 2012-10-16 09:20:19
@Julius:您的代码太复杂了,包含了几个bug。我只对前几行作更正:
- (NSBitmapImageRep *)bitmapImageRepresentation
{
for( NSImageRep *rep in [self representations] )
if( [rep isKindOfClass:[NSBitmapImageRep class]] ) return rep;
return nil;
}
这将提取第一个NSBitmapImageRep
,如果它是表示中的成员,或者返回零,如果没有NSBitmapImageRep
。我将给出另一种解决方案,无论表示中的NSImageReps
是什么,它都能工作:NSBitmapImageRep
、NSPDFImageRep
或NSCGImageSnapshotRep
或.
- (NSBitmapImageRep *)bitmapImageRepresentation
{
CGImageRef CGImage = [self CGImageForProposedRect:nil context:nil hints:nil];
return [[[NSBitmapImageRep alloc] initWithCGImage:CGImage] autorelease];
}
或者,为了避免NSImage的子类,您可以编写:
NSImage *img = [[[NSImage alloc] initWithContentsOfFile:filename] autorelease];
CGImageRef CGImage = [img CGImageForProposedRect:nil context:nil hints:nil];
NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithCGImage:CGImage] autorelease];
这将只返回一个NSBitmapImageRep
,如果图像包含多个表示(例如,来自TIFF文件的大量NSBitmapImageRep
),这可能不够好。但是添加一些代码很简单。
发布于 2017-05-16 19:47:28
也许聚会晚了,但这是@mrwalker回复中的Swift 3版本:
extension NSImage {
func bitmapImageRepresentation(colorSpaceName: String) -> NSBitmapImageRep? {
let width = self.size.width
let height = self.size.height
if width < 1 || height < 1 {
return nil
}
if let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(width), pixelsHigh: Int(height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: colorSpaceName, bytesPerRow: Int(width) * 4, bitsPerPixel: 32)
{
let ctx = NSGraphicsContext.init(bitmapImageRep: rep)
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.setCurrent(ctx)
self.draw(at: NSZeroPoint, from: NSZeroRect, operation: NSCompositingOperation.copy, fraction: 1.0)
ctx?.flushGraphics()
NSGraphicsContext.restoreGraphicsState()
return rep
}
return nil
}
}
https://stackoverflow.com/questions/12896831
复制相似问题