UIImage image operations

Source: Internet
Author: User

There are some classes in UIKit that can be used to manipulate a single image, and an image class that can be used to display an image. Apple also provides a special navigation controller for selecting images from the image repository.

The UIImage class encapsulates the image and its underlying data. It can be directly drawn in one view, or used as an image container in another larger image view container. This class provides methods to load images from various sources, set the image direction on the screen, and provide information about the images. For simple graphics applications, you can use the UIImage object in the drawRect method of the View class to draw images and group templates.

You can use files for initialization, URLs, raw data, or the content of a Core Graphics image. Both static methods (class methods) and instance methods are available. These methods can reference and cache the existing image content, or instance new image objects. How to Use them depends entirely on the needs of the application.

The simplest way to use an image is to use the static method. Static methods do not manage image instances. In contrast, they provide direct interfaces that can be used to share memory cache objects located inside the framework. This helps keep your applications clean and tidy. Both static and instance methods can be used to create the same object.

1. Create a file (static method)

Source code printing?
UIImage * myImage = [UIImage imageNamed: @ "ppp"];
Ii. Use URL and raw data (static method)

Source code printing?
NSData * imageData = [NSData initWithBytes: image: imagePtr length: imageSize]; // assume that imagePtr is a pointer to the original data.
UIImage * myImage = [[UIImage alloc] initWithData: imageData];
Source code printing?
UIImage * myImage2 = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @ "http://www.bkjia.com/uploadfile/2012/1023/20121023093227604.png"];
3. Use Core Graphics (static method)

Source code printing?
UIImage * myImage3 = [UIImage imageWithCGImage: myCGImageRef];
4. Use Files (instance method)

Source code printing?
UIImage * myImage4 = [[UIImage alloc] initWithContentsOfFile: [NSString stringWithFormat: @ "% @/Documents/ppp.png", NSHomeDirectory ()];
5. Use URL and raw data (instance method)

If the image is stored in the memory, you can create an NSData object as the original input of the initWithData method to initialize a UIImage object.

If the image is a network image, you can use NSData for preloading and use it to initialize the UIImage object:

Source code printing?
UIImage * myImage5 = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @ "http://www.bkjia.com/uploadfile/2012/1023/20121023093227604.png"];
6. Use Core Graphics (instance method)


Source code printing?
UIImage * myImage6 = [[UIImage alloc] initWithCGImage: myCGImageRef];
7. display images

When the drawRect method of the View class is invoked, they call the internal callback routine. Unlike other image classes, a UIImage object cannot be regarded as a child and directly attached to other views because it is not a view class. In turn, a UIView class can call the drawRect method of the image in the drawRect routine of the view. This allows the image to appear in the display area of the UIView class.

The drawRect method can be called as long as some parts of a view object window need to be drawn. To display the content of a UIImage in the window, call the drawRect method of the object:

Source code printing?
-(Void) drawRect :( CGRect) rect {
CGRect myRect;

MyRect. origin. x = 0.0;
MyRect. origin. y = 0.0;
MyRect. size = myImage. size;
[MyImage drawInRect: myRect];
}
Do not assign any new object in the drawRect method, because it is called every time the window is re-painted.
The drawRect method is called only when the view is first drawn. To force update, you can use the setNeedsDisplay or setNeedsDisplayInRect method of the View class:

Source code printing?
[MyView setNeedsDisplay];
[MyView setNeedsDisplayInRect: self. view];
8. Draw a pattern

If the image is a pattern template, you can use another method provided by the UIImage class to draw the image again in the entire view area:


Source code printing?
UIView * myView = [[UIView alloc] initWithFrame: CGRectMake (0, 0,200,200)];
[MyImage drawInRect: myView. frame];
[Self. view addSubview: myView]; <span> </span>
9. Direction

The orientation of an image determines how it is rotated on the screen. Because the iPhone can be held in 6 different ways, it is necessary to rotate the image when the direction changes. UIImage has a read-only attribute imageOrientation to identify its direction.

Source code printing?
UIImageOrientation myOrientation = myImage. imageOrientation;
You can set the following directions:
Source code printing?
Typedef enum {
UIImageOrientationUp, // default orientation default direction
UIImageOrientationDown, // 180 deg rotation 180 degrees
UIImageOrientationLeft, // 90 deg CCW clockwise rotation 90 degrees
UIImageOrientationRight, // 90 deg CW clockwise rotation 90 degrees
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip up horizontally
UIImageOrientationDownMirrored, // horizontal flip down horizontally
UIImageOrientationLeftMirrored, // vertical flip Rotate 90 degrees counterclockwise and flip vertically
UIImageOrientationRightMirrored, // vertical flip rotates 90 degrees clockwise, vertical flip
} UIImageOrientation;
10. Image Size

You can use the size attribute to read the size of an image and obtain a CGSize structure, which contains wifth and height.

Source code printing?
CGSize myImageSize = myImage. size;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.