Search code examples
objective-cmacoscocoansimage

How to compress and scale down an NSImage?


This is my compress code

NSBitmapImageRep* tmpRep = [[_image representations] objectAtIndex:0];
[tmpRep setPixelsWide:512];
[tmpRep setPixelsHigh:512];
[tmpRep setSize:NSMakeSize(SmallThumbnailWidth, SmallThumbnailHeight)];
NSDictionary* imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:NSImageCompressionFactor];
NSData* outputImageData = [tmpRep
                           representationUsingType:NSJPEGFileType properties:imageProps];
NSString* imageFilePath = [NSString stringWithFormat:@"%@/thumbnail.jpg",imagePath];
[outputImageData writeToFile:imageFilePath atomically:YES];

The original image size is 960*960.I want to compress the original image into 512*512.But the output Image's size is 960*960 when I check it in finder and the location size which compares with the original has really been compressed.Any one could tell me why ? thank you


Solution

  • Try this one:

    This will reduce the saving size in kbs:

    -(NSImage *)imageCompressedByFactor:(float)factor{
        NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:[self TIFFRepresentation]];
        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:factor] forKey:NSImageCompressionFactor];
        NSData *compressedData = [imageRep representationUsingType:NSJPEGFileType properties:options];
        return [[NSImage alloc] initWithData:compressedData];
    }
    

    This will reduce the file size in pixels : Copied from here

    @implementation NSImage (ProportionalScaling)
    
    - (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize{
      NSImage* sourceImage = self;
      NSImage* newImage = nil;
    
      if ([sourceImage isValid]){
        NSSize imageSize = [sourceImage 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 ) == NO )
        {
    
          float widthFactor  = targetWidth / width;
          float heightFactor = targetHeight / height;
    
          if ( widthFactor < heightFactor )
            scaleFactor = widthFactor;
          else
            scaleFactor = heightFactor;
    
          scaledWidth  = width  * scaleFactor;
          scaledHeight = height * scaleFactor;
    
          if ( widthFactor < heightFactor )
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
    
          else if ( widthFactor > heightFactor )
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
        newImage = [[NSImage alloc] initWithSize:targetSize];
        [newImage lockFocus];
          NSRect thumbnailRect;
          thumbnailRect.origin = thumbnailPoint;
          thumbnailRect.size.width = scaledWidth;
          thumbnailRect.size.height = scaledHeight;
          [sourceImage drawInRect: thumbnailRect
                         fromRect: NSZeroRect
                        operation: NSCompositeSourceOver
                         fraction: 1.0];
        [newImage unlockFocus];
      }
      return [newImage autorelease];
    }
    @end