Trying to resize NSImage, which turns into NSData

I have an NSImage that I am trying to modify this way:

NSImage *capturePreviewFill = [[NSImage alloc] initWithData:previewData]; NSSize newSize; newSize.height = 160; newSize.width = 120; [capturePreviewFill setScalesWhenResized:YES]; [capturePreviewFill setSize:newSize]; NSData *resizedPreviewData = [capturePreviewFill TIFFRepresentation]; resizedCaptureImageBitmapRep = [[NSBitmapImageRep alloc] initWithData:resizedPreviewData]; saveData = [resizedCaptureImageBitmapRep representationUsingType:NSJPEGFileType properties:nil]; [saveData writeToFile:@"/Users/ricky/Desktop/Photo.jpg" atomically:YES]; 

My first problem is that my image is crushed when I try to resize it and disagree with the aspect ratio. I read that using -setScalesWhenResized will solve this problem, but it is not.

The second problem is that when I try to write the image to a file, the image does not actually change at all.

Thanks in advance, Ricky.

+4
source share
3 answers

I found this blog post very useful for resizing my image: http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/

You will need to ensure that the image format is consistent when resizing the image; this will not be done for you. So I did this when I tried to put the image in the print area on default paper:

 NSImage *image = ... // get your image NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo]; NSSize paperSize = printInfo.paperSize; CGFloat usablePaperWidth = paperSize.width - printInfo.leftMargin - printInfo.rightMargin; CGFloat resizeWidth = usablePaperWidth; CGFloat resizeHeight = usablePaperWidth * (image.size.height / image.size.width); 

Here is a slightly modified version of his blog code:

 NSData *sourceData = [image TIFFRepresentation]; float resizeWidth = ... // your desired width; float resizeHeight = ... // your desired height; NSImage *sourceImage = [[NSImage alloc] initWithData: sourceData]; NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(resizeWidth, resizeHeight)]; NSSize originalSize = [sourceImage size]; [resizedImage lockFocus]; [sourceImage drawInRect: NSMakeRect(0, 0, resizeWidth, resizeHeight) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0]; [resizedImage unlockFocus]; NSData *resizedData = [resizedImage TIFFRepresentation]; [sourceImage release]; [resizedImage release]; 
+10
source

If you can require Mac OS X 10.6 or later, send the image CGImageForProposedRect:context:hints: write CGImage using the CGImageDestination object.

The rectangle should have NSZeroPoint as its start, and its size should be the right size.

This will not scale the image proportionally (maintaining aspect ratio); you have to do it yourself.

+1
source

A way to 10.6 for this (without going through the TIFF view) is to lock the focus on the resized image , create an NSBitmapImageRep for the image degree (i.e. a rectangle with zero start and image size), unlock focus, and then ask what image of the bitmap for JPEG data.

0
source

Source: https://habr.com/ru/post/1305337/


All Articles