Cocoa 如何在没有透明度的情况下缩放 NSImage
In Cocoa how to scale a NSImage with no transparency
我正在使用此代码缩放图像,一切正常,但生成的图像在其属性中具有透明度,我只需要一个没有透明度且根本没有 alpha 通道的图像...任何人都知道如何我可以修改我的代码以获得没有透明胶片的图像吗?
这是我的代码:
- (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;
}
我找到了解决方案...我更改了 representationUsingType...我使用了 JPEGFileType 因此图像完全没有透明度...
这里是更改后的代码:
NSData *dataToWrite = [rep representationUsingType:NSJPEGFileType properties:nil];
我正在使用此代码缩放图像,一切正常,但生成的图像在其属性中具有透明度,我只需要一个没有透明度且根本没有 alpha 通道的图像...任何人都知道如何我可以修改我的代码以获得没有透明胶片的图像吗? 这是我的代码:
- (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;
}
我找到了解决方案...我更改了 representationUsingType...我使用了 JPEGFileType 因此图像完全没有透明度...
这里是更改后的代码:
NSData *dataToWrite = [rep representationUsingType:NSJPEGFileType properties:nil];