• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Scaling Images
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scaling Images


  • Subject: Re: Scaling Images
  • From: David Trevas <email@hidden>
  • Date: Tue, 26 Jun 2001 00:06:46 -0500

On Sunday, June 24, 2001, at 01:33 AM, Michael Horn wrote:

I am trying to read in an image, scale it, and then save the scaled
version as a new file. Can someone point me to either an example
that already does this or just point me in the right direction?


Hey, Michael,

I was very interested in your question, because I've been working for a while on the same thing. I was hoping someone would give you the answer so I could have it, too. Seeing that people were more interested in debating about pointer, I kept plugging away and finally solved it. It turns out that you need to be comfortable with the relationship between NSImages and NSImageReps (and the specialized versions like NSBitmapImageRep).

- (void)scalePicture
{
NSBitmapImageRep *myBitmapRep;
NSData *inData, *outData;
NSSize desiredSize;
NSImage *myImage;
float desiredWidth = 212.0, desiredHeight = 156.0;

desiredSize.width = desiredWidth;
desiredSize.height = desiredHeight;

inData = [[NSData alloc] initWithContentsOfFile:@"/Users/myname/Documents/OriginalPicture.tiff"]; //Needs to be released
if(!inData) return;

myBitmap = [NSBitmapImageRep imageRepWithData:picData]; //Automatically auto-released
if(!myBitmap) return;

myImage = [[NSImage alloc] initWithSize:desiredSize]; //Needs to be released
if(!myImage) return;

[myImage setScalesWhenResized:YES];
[myImage addRepresentation:myBitmap];

[myImage lockFocusOnRepresentation:myBitmap];
outData = [myImage TIFFRepresentation]; //Automatically auto-released
[outData writeToFile:@"/Users/myname/Documents/NewPicture.tiff" atomically:NO];
[myImage unlockFocus];

[myImage release];
[inData release];
}

1. Set the dimensions you want your new picture to be.
2. Get the data of the Original Picture as an NSData object.
3. Create an NSBitmapImageRep from that data. This image rep(resentation) is the original size of the Original Picture.
4. Create an NSImage using the initWithSize: method using the desired size. This creates essentially an empty box waiting for you to pour something in it. Notice that most of the other init methods with NSImage use some data to get started.
5. Set the "ScalesWhenResized" flag to YES so that when you pour the image rep into the image, it will scale itself to the image's size.
6. Add the NSBitmapImageRep to the NSImage.
7. Lock the focus on the new representation. NSImage maintains an array of representations, so you have to tell it the one you want to focus on at any given time.
8. Create the output data by taking the TIFF representation of the NSImage. Since you focused on the bitmap image, it is the one that gets copied into data.
9. Write the data to the new file.
10. Unlock the focus on the bitmap rep.
11. Release objects created by an alloc-init sequence.

I hope my understanding of NSImageReps and NSImages is correct. It seems to be effective, but hardly anything is more disturbing than to have something work for the wrong reasons.

I hope this helps.

Dave


  • Prev by Date: Re: PB widget
  • Next by Date: Re: Using file types in Document Based Applications...
  • Previous by thread: Scaling images
  • Next by thread: Matrix of NSTextFields (creating it in IB)
  • Index(es):
    • Date
    • Thread