在Cocoa中,如何在没有透明度的情况下缩放NSImage
我用这段代码缩放一幅图像,一切正常,但生成的图像在其属性中有透明度,我只需要一幅没有透明度和alpha通道的图像。。。有人知道我如何修改代码以获得没有透明胶片的图像吗? 这是我的代码:在Cocoa中,如何在没有透明度的情况下缩放NSImage,cocoa,transparency,nsimage,alpha-transparency,Cocoa,Transparency,Nsimage,Alpha Transparency,我用这段代码缩放一幅图像,一切正常,但生成的图像在其属性中有透明度,我只需要一幅没有透明度和alpha通道的图像。。。有人知道我如何修改代码以获得没有透明胶片的图像吗? 这是我的代码: - (void)scalePreviews:(NSString *)outputPath :(NSURL *)nomeImmagine :(CGFloat)size1 :(CGFloat)size2 :(NSString *)nomeIcona { NSImage *image = [[NSImage all
- (void)scalePreviews:(NSString *)outputPath :(NSURL *)nomeImmagine :(CGFloat)size1 :(CGFloat)size2 :(NSString *)nomeIcona
{
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[nomeImmagine path]];
if (!image)
image = [[NSWorkspace sharedWorkspace] iconForFile:[nomeImmagine path]];
NSSize outputSize = NSMakeSize(size1,size2);
NSImage *anImage = [self scaleImagePreviews:image toSize:outputSize];
NSString *finalPath = [outputPath stringByAppendingString:[NSString stringWithFormat:@"/image-previews/%@",nomeIcona]];
//questo serve a creare la directory delle icone se gia' non esiste
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:[outputPath stringByAppendingString:@"/image-previews"] isDirectory:nil])
if(![fileManager createDirectoryAtPath:[outputPath stringByAppendingString:@"/image-previews"] withIntermediateDirectories:YES attributes:nil error:NULL])
NSLog(@"Error: Create folder failed %@", [outputPath stringByAppendingString:@"/image-previews"]);
// NSLog(@"finalPath: %@",finalPath);
NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
[dataToWrite writeToFile:finalPath atomically:NO];
}
- (NSImage *)scaleImagePreviews:(NSImage *)image toSize:(NSSize)targetSize
{
if ([image isValid])
{
NSSize imageSize = [image size];
float width = imageSize.width;
float height = imageSize.height;
float targetWidth = targetSize.width;
float targetHeight = targetSize.height;
float scaleFactor = 0.0;
float scaledWidth = targetWidth;
float scaledHeight = targetHeight;
NSPoint thumbnailPoint = NSZeroPoint;
if (!NSEqualSizes(imageSize, targetSize))
{
float widthFactor = targetWidth / width;
float heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
{
scaleFactor = widthFactor;
}
else
{
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
newImage = [[NSImage alloc] initWithSize:targetSize];
[newImage lockFocus];
NSRect thumbnailRect;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = targetSize.width; //scaledWidth;
thumbnailRect.size.height = targetSize.height; //scaledHeight;
[image drawInRect:thumbnailRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
[newImage unlockFocus];
}
}
return newImage;
}
我找到了一个解决办法。。。我用类型更改了表示。。。我使用了JPEG文件类型,所以图像根本没有透明胶片 以下是更改后的代码: NSData*dataToWrite=[rep rep representationUsingType:NSJPEGFileType属性:nil]
你所说的透明性是什么意思?您希望在原始图像透明的地方使用什么颜色?在绘制原始图像之前,您是否尝试过用该颜色填充矩形?e、 g.[NSColor whiteColor]集];NSRectFillthumbnailRect;我试图使用你的代码,但我仍然有那个问题。。。对于其属性中的透明度,我的意思是,当我在一个只接受没有透明度和alpha通道的图像的站点中使用图像时,如果图像没有透明度,我会得到一个错误,说它找到了透明度。。。所以我猜它一定是在它的DNA里。。。我不知道怎么。。。我还注意到,如果我用Photoshop打开图像,并发出“展平图像”命令,问题就会消失。。。