Resize picture
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.
Sometimes you might need to resize a picture because it’s too big or too small to show on your iPhone or iPad app. There are many ways to accomplish this task and I would like to discuss one of the ways we are using here at Walden Systems, because we believe this is the most straightforward method to resize any given image.
Strategy to resize a picture is fairly simple:
First, we need to determine what final destination the picture you want to resize will be. Second, we need to create a rectangle for that size. Third, create an empty image object based on your new size. Fourth, superimpose your existing image onto your new image object.
Here are actual code samples:
The first thing you need to do is to set the size of the new image, to do so, use the following line:
UIGraphicsBeginImageContext ( SOME_WIDTH, SOME_HEIGHT )
Then you add the picture giving it starting x and y coordinates and width and height.
[YOUR_INSTANCE_IMAGE_OBJ_PTR drawInRect:CGRectMake( START_X, START_Y, SOME_WIDTH, SOME_HEIGHT)];
Then you will need to set a new image to an UIImage object
UIImage *NEW_DESTINATION_IMAGE = UIGraphicsGetImageFromCurrentImageContext();
Finally,
UIGraphicsEndImageContext()
So, this is how a final code for resizing your picture will look like:
1 - (UIImage )resizeImage:(UIImage )orig_image convertToSize:(CGSize) new_size 2 { 3 UIGraphicsBeginImageContext(new_size); 4 [image drawInRect:CGRectMake(0, 0, new_size.width, new_size.height)]; 5 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 6 UIGraphicsEndImageContext(); 7 return newImage; 8 }