iOS development: Uiimageview Common operations

Source: Internet
Author: User

Uiimageview, as the name implies, is used to place pictures. When using the Interface Builder design interface, of course, you can drag the control directly into and set the relevant properties, this is not said, here is the code.

1. Create a Uiimageview:

There are five ways of creating a Uiimageview object:

Uiimageview *imageview1 = [[Uiimageview alloc] init]; Uiimageview *imageview2 = [[Uiimageview alloc] initWithFrame: (CGRect)]; Uiimageview *IMAGEVIEW3 = [[Uiimageview alloc] Initwithimage: (UIImage *)]; Uiimageview *imageview4 = [[Uiimageview alloc] Initwithimage: (UIImage *) Highlightedimage: (UIImage *)]; Uiimageview *imageview5 = [[Uiimageview alloc] Initwithcoder: (Nscoder *)];

The more commonly used is the front three. As for the fourth, when the highlighted attribute of this imageview is yes, the parameter highlightedimage is displayed, and the first parameter uiimage is typically displayed.

2. Frame and Bounds properties:

The second method of creating a Uiimageview method is to set the position and size at the time of creation.

When you want to change the position later, you can reset the Frame property:

Imageview.frame = CGRectMake (CGFloat x, cgfloat y, cgfloat width, cgfloat heigth);

Notice that Uiimageview also has a bounds property

Imageview.bounds = CGRectMake (CGFloat x, cgfloat y, cgfloat width, cgfloat heigth);

So what's the difference between this property and frame?

My understanding is that frame sets its position and size, and bounds can only set its size, and the X and y in its parameters do not work even if the frame property was not previously set, the final position of the control is not the parameter set by bounds. The bounds implementation is to scale the Uiimageview control to the center of the original center. For example, the following code is available:

Imageview.frame = CGRectMake (0, 0, 100, 460); imageview.bounds = CGRectMake (100,, 160, 230);

After execution, the position and size of this imageview are (80, 115, 160, 230).

3. Contentmode Properties:

This property is used to set the display of the image, such as centering, right, whether zooming, etc., there are several constants to set:

Uiviewcontentmodescaletofilluiviewcontentmodescaleaspectfituiviewcontentmodescaleaspectfilluiviewcontentmoderedrawuiviewc Ontentmodecenteruiviewcontentmodetopuiviewcontentmodebottomuiviewcontentmodeleftuiviewcontentmoderightuiviewcontentmodeto Pleftuiviewcontentmodetoprightuiviewcontentmodebottomleftuiviewcontentmodebottomright

Note that the above several constants, usually without scale, when the picture size exceeds imageview size, only part of the display in the ImageView. The Uiviewcontentmodescaletofill property causes the picture to deform. Uiviewcontentmodescaleaspectfit will ensure that the picture scale is the same, and that it all appears in ImageView, which means that ImageView will be partially blank. Uiviewcontentmodescaleaspectfill will also be the same as the picture, but it is filled with the entire imageview, it is possible that only some of the pictures are displayed.

Top three effects such as:

Uiviewcontentmodescaletofill Uiviewcontentmodescaleaspectfit Uiviewcontentmodescaleaspectfill

4. Change the location

Change the location of a uiimageview, you can

4.1 Modify its Frame property directly

4.2 Modify its Center property:

Imageview.center = Cgpointmake (CGFloat x, cgfloat y);

The Center property refers to the middle point of the ImageView.

4.3 Using the Transform property

Imageview.transform = cgaffinetransformmaketranslation (cgfloat dx, cgfloat dy);

where dx and dy indicate how much you want to move in the X or Y direction, rather than how much to move.

5. Rotate the image

Imageview.transform = cgaffinetransformmakerotation (cgfloat angle);

Note that it rotates in a clockwise direction, and the center of the rotation is the center of the original ImageView, which is the position represented by the Center property.

The parameter of this method is angle in radians, not our most commonly used degree, so you can write a macro definition:

#define Degreestoradians (x) (m_pi* (x)/180.0)

Used to convert degrees to radians. Is the case of 45 degrees of rotation:

6. Zoom image

Or use the Transform property:

Imageview.transform = Cgaffinetransformmakescale (cgfloat scale_w, CGFloat scale_h);

Among them, CGFloat Scale_w and CGFloat Scale_h respectively, the original width and height of the scale to how many times, is scaled to the original 0.6 times times:

7. Play a series of pictures

Imageview.animationimages = imagesarray;//Set all the pictures in how many seconds to play imageview.animationduration = [Imagesarray count];// Do not repeat how many times, 0 means countless times Imageview.animationrepeatcount = 0;//start playing [ImageView startanimating];

Where Imagesarray is an array of some column images. Such as:

8. Add a click event for the Picture:

imageview.userinteractionenabled = YES; UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Tapimageview:)]; [ImageView Addgesturerecognizer:singletap];

Be sure to set userinteractionenabled to Yes before you can respond to the click event.

9. Other Settings

imageview.hidden = yes or no;    // Hide or Show picture Imageview.alpha = (cgfloat) al;    //set Transparency Imageview.highlightedimage = (UIImage *) Hightlightedimage; Set the picture that is displayed when highlighting imageview.image = (UIImage *) image;//set the picture to display properly [ImageView sizetofit];    // Adjust the picture size to the same as the content picture 
Related Article

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.