Preliminaries:
UIImage
It is an object that specifically stores image data. The default compatible image format is PNG. You can obtain an image object from a file, a Quartz image object, or image Data data.
UIImage has many related functions. In addition to representing the image data, it can also process the data in the image to produce different image effects.
UIImage creation
There are many ways to load images with UIImage, the most commonly used are the following four:
First, get the picture from the current project directory, use the imageNamed function
[UIImage imageNamed: ImageName];
[UIImage imageNamed: @ "a.png"];
Second, get the picture from the database, load it with NSData, generally use the picture to read from the database, for example:
NSString * filePath = [[NSBundle mainBundle] pathForResource: fileName ofType: extension];
NSData * image = [NSDatadataWithContentsOfFile: filePath];
[UIImage imageWithData: image];
Third, to get the picture from the file directory, use [UIImage imageWithContentOfFile:] or [imageinitWithContentOfFile:]
NSString * filePath = [[NSBundle mainBundle] pathForResource: fileName ofType: @ "picture extension"];
[UIImage imageWithContentsOfFile: aImagePath];
// Image of icon.png in the binding folder
NSString * path = [[NSBundle mainBundle] pathForResource: @ ”icon” ofType: @ ”png”];
NSImage * myImage = [UIImageimageWithContentsOfFile: path];
Fourth, get pictures from the Internet
UIImage * image = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL [NSURLURLWithString: @ "http://www.2cto.com/uploadfile/2013/0702/20130702085459778.jpg"]];
UIImageView * imageView = [[UIImageView alloc] initWithImage: image];
UIImageView
UIImageView: You can load images through UIImage and assign them to UIImageView. After loading, you can specify the display position and size.
1.Init
UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (0.0,45.0,300,300)];
imageView.image = [UIImageimageNamed: @ "a.png"]; // Load the image
[self.view addSubView: image];
[imageView release];
The // imageNamed method cannot load images by path. This method is prone to cause memory warnings and automatic exit problems.
// It is best to solve this problem by directly reading the file path [UIImageimageWithContentsOfFile].
NSImage * image = [[NSImagealloc] initWithContentsOfURL: (NSURL *)];
NSImage * image = [[NSImagealloc] initWithContentsOfFile: (NSString *)];
// Make a UIImageView respond to a click event
// Create a picture area of a specified size
UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0,320, 44)];
// Allow the user to operate the area
imgView.userInteractionEnabled = YES;
// Create the user's "tap gesture" response, and specify it with @selector (), the method called after the user clicks
UITapGestureRecognizer * singleTap = [[UITapGestureRecognizeralloc] initWithTarget: selfaction: @selector (onClickImage)];
// Add gesture object to view object
[imgView addGestureRecognizer: singleTap];
// Release resources
[singleTap release];
-(void) onClickImage {
// here, do whatever you wantto do
NSLog (@ "imageview is clicked!");
}
1) When loading with imageNamed, the system will cache the image into memory. If the image is large or there are many images, this method will consume a lot of memory, and releasing the memory of the image is a relatively troublesome thing. For example: if you use imageNamed to load an image into a dynamic array NSMutableArray, and then assign the array to an UIView object's animationImages for frame-by-frame animation, then this will likely cause a memory leak. And releasing the memory occupied by the image will not be so simple. But loading images with imageNamed also has its own advantages. For the same image system, it will only be cached to memory once, which is very advantageous for image reuse. For example: If you need to load the same icon repeatedly in a TableView, then using imageNamed to load an image, the system will cache that icon to memory. Each time the image is used in the Table, the picture pointer will only point to the same memory. In this case, loading images using imageNamed becomes very effective.
2) When loading with NSData, the image will be loaded into the program by the system in data mode. When you do not need to reuse the image, or you need to store the image as a data in the database, or you want to download a large image through the network, please use imageWithData to load the image as much as possible.
Use NSTimer to achieve the effect of Apple's UIImageView animation (this is a solution)
Expansion:
Add gestures to UIImageView
[ImageView addGestureRecognizer: our own gesture recognizer];
How do I create a gesture? Look at the usage of UITapGestureRecognizer
// single finger click
UITapGestureRecognizer * singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget: self
action: @selector (handleSingleFingerEvent :)];
singleFingerOne.numberOfTouchesRequired = 1; // Hand index
singleFingerOne.numberOfTapsRequired = 1; // tap times
singleFingerOne.delegate = self;
// Double finger
UITapGestureRecognizer * singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget: self
action: @selector (handleSingleFingerEvent :)];
singleFingerTwo.numberOfTouchesRequired = 1;
singleFingerTwo.numberOfTapsRequired = 2;
singleFingerTwo.delegate = self;
// Two-finger click
UITapGestureRecognizer * doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget: self
action: @selector (handleDoubleFingerEvent :)];
doubleFingerOne.numberOfTouchesRequired = 2;
doubleFingerOne.numberOfTapsRequired = 1;
doubleFingerOne.delegate = self;
UITapGestureRecognizer * doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget: self
action: @selector (handleDoubleFingerEvent :)];
doubleFingerTwo.numberOfTouchesRequired = 2;
doubleFingerTwo.numberOfTapsRequired = 2;
doubleFingerTwo.delegate = self;
// If you do not add the following, when single-finger double-click, the processing in single-finger double-click will be called first, and then the processing in single-finger double-click
[singleFingerOne requireGestureRecognizerToFail: singleFingerTwo];
// Same for two fingers
[doubleFingerOne requireGestureRecognizerToFail: doubleFingerTwo];
[self.view addGestureRecognizer: singleFingerOne];
[self.view addGestureRecognizer: singleFingerTwo];
[self.view addGestureRecognizer: doubleFingerOne];
[self.view addGestureRecognizer: doubleFingerTwo];
[singleFingerOne release];
[singleFingerTwo release];
[doubleFingerOne release];
[doubleFingerTwo release];
Method to handle events, code:
// Handle single finger events
-(void) handleSingleFingerEvent: (UITapGestureRecognizer *) sender
{
if (sender.numberOfTapsRequired == 1) {
// single finger click
NSLog (@ "single finger click");
} elseif (sender.numberOfTapsRequired == 2) {
// Double finger
NSLog (@ "single-finger double-click");
}
}
// Handle two-finger events
-(void) handleDoubleFingerEvent: (UITapGestureRecognizer *) sender
{
if (sender.numberOfTapsRequired == 1) {
// Two-finger click
NSLog (@ "two-finger click");
} elseif (sender.numberOfTapsRequired == 2) {
// Double-tap with two fingers
NSLog (@ "Double-finger double click");
}
}