在Cocoa中如何在没有透明度的情况下扩展NSImage

时间:2015-01-04 20:26:22

标签: cocoa transparency nsimage alpha-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;

}

1 个答案:

答案 0 :(得分:0)

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

这里更改了代码:

  

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