Search code examples
cocoatransparencynsimagealpha-transparency

In Cocoa how to scale a NSImage with no transparency


I'm using this code to scale an image, everything works fine but the resulting image has a transparency in its properties and I just need an image with no transparencies and no alpha channels at all... Anyone knows how could I modify my code to obtain an image with no transparencies? Here my code:

- (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;

}

Solution

  • I found a solution... I changed the representationUsingType... I used JPEGFileType so the image has no transparencies at all...

    here the changed code:

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