【问题标题】:In Cocoa how to scale a NSImage with no transparency在 Cocoa 中如何缩放没有透明度的 NSImage
【发布时间】:2015-01-04 20:26:22
【问题描述】:

我正在使用此代码来缩放图像,一切正常,但生成的图像在其属性中具有透明度,我只需要一个没有透明度且根本没有 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;

}

【问题讨论】:

  • “属性的透明度”是什么意思?您想在原始图像透明的地方使用什么颜色?在绘制原始图像之前,您是否尝试过用该颜色填充矩形?例如[[NSColor whiteColor] set]; NSRectFill(thumbnailRect);
  • 我尝试使用您的代码,但我仍然有这个问题......“其属性的透明度”我的意思是,如果图像在我使用它时没有透明度的网站只接受没有透明胶片和 Alpha 通道的图像我收到一条错误消息,说它找到了透明胶片……所以我猜它一定在它的“DNA”中……我不知道怎么……我还注意到,如果我打开用 Photoshop 制作图像,然后我给出命令“拼合图像”,问题就消失了……

标签: cocoa transparency nsimage alpha-transparency


【解决方案1】:

我找到了解决方案...我更改了representationUsingType...我使用了JPEGFileType,因此图像根本没有透明胶片...

这里是修改后的代码:

NSData *dataToWrite = [rep representationUsingType:NSJPEGFileType properties:nil];

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 2012-04-15
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多