How to use Ios--uiimageview

Source: Internet
Author: User

How to use Ios--uiimageview//initialization
uiimageview *imageview=[[uiimageview alloc] Initwithframe:cgrectmake (n., +)]; //need to set picture UIImage the first kind: [ImageView setimage:[uiimage imagenamed:@ "1.jpeg"]; //second type:nsstring *filepath=[[nsbundle Mainbundle] pathforresource:@ "1" oftype:@ "JPEG"];UIImage *images=[uiimage Imagewithcontentsoffile:filepath];//[imageview setimage:images]; //Third type:NSData *data=[nsdata Datawithcontentsoffile:filepath];UIImage *image2=[uiimage Imagewithdata:data];[ImageView Setimage:image2];

Among them, the 12th type is one, altogether two kinds:

1) when loading in imagenamed mode, the system caches the image to memory. If the image is larger, or the image is more, it consumes a lot of memory in this way, 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 animate the array to a animationimages of an UIView object, then this is likely to cause a memory leak. And the memory that is occupied by releasing the image is not that simple. But using imagenamed to load images also has its own advantages. The same image system will only cache it to memory once, which is very advantageous for the reuse of images. For example: You need to reload the same icon in a tableview, then load the image with Imagenamed, the system will cache the icon to memory, in the table every time the image is used, only the picture pointer to the same piece of memory. In this case, loading the image using imagenamed becomes very effective.

2) when loading with NSData mode, the image is loaded into the program by the system in data mode. When you don't need to reuse the image, or if you need to store the image as data in a database, or if you want to download a large image over the network, try to load the image using Imagewithdata.

No matter which way the image is loaded, after the image is used, be sure to remember to show free memory.

 //Uiimageview Common methods

UIImage *oneimage = [UIImage imagenamed:@ "Max.png"]; Use ImageView to find a picture by name

Uiimageview *oneimageview = [[Uiimageview alloc] initwithimage:oneimage]; Add the Oneimage to the Oneimageview

Oneimageview.frame = CGRectMake (10, 10, 300, 300); Set picture position and size

Oneimageview.bounds = CGRectMake (10, 10, 280, 280); Set the position and size of the picture, if the frame is set, then it will not work

Oneimageview.backgroundcolor = [Uicolor Redcolor]; Set Background color

Oneimageview.alpha = 1.0; Set transparency

Oneimageview.contentmode = Uiviewcontentmodetop;

Have the following relative positional relationships

Uiviewcontentmodescaletofill

Uiviewcontentmodescaleaspectfit

Uiviewcontentmodescaleaspectfill

Uiviewcontentmoderedraw

Uiviewcontentmodecenter

Uiviewcontentmodetop

Uiviewcontentmodebottom

Uiviewcontentmodeleft

Uiviewcontentmoderight

Uiviewcontentmodetopleft

Uiviewcontentmodetopright

Uiviewcontentmodebottomleft

Uiviewcontentmodebottomright

Oneimageview.center = Cgpointmake (150, 300); Change the location of the picture center

Oneimageview.transform = Cgaffinetransformmaketranslation (20, 20); Move a picture a distance where 20 indicates how much you want to move in the X or Y direction, rather than how much to move.

Oneimageview.transform = Cgaffinetransformmakerotation (0.0f); Rotate the image a certain angle note: units are radians, not our most commonly used degrees, so you can write a macro definition: #define Degreestoradians (x) (m_pi* (x)/180.0)

Oneimageview.transform = Cgaffinetransformmakescale (0.5, 0.5); 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.5 times times

Add a click event to a picture

Be sure to set the userinteractionenabled to Yes before responding to the Click event

oneimageview.userinteractionenabled = YES; Set up pictures to interact

UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Tapimageview:)]; Set gestures

[Oneimageview Addgesturerecognizer:singletap]; Add a pack to a picture

Oneimageview.hidden = NO; Hide or Show picture Yes to hide

[Oneimageview SizeToFit]; Resize the picture to be the same as the content picture

Oneimageview.highlightedimage = (UIImage *) hightlightedimage; Set the picture to display when highlighting

Set the picture to play continuously, achieve the animation effect

Oneimageview.animationimages = [Nsarray arraywithobjects:[uiimageimagenamed:@ "Max.png"], [UIImage imageNamed:@] Min.png "], nil];

Oneimageview.animationduration = 0.3f; Set the time to cycle once

Oneimageview.animationrepeatcount = 0; The number of cycles. Set to 0 o'clock Wireless loop

[Oneimageview startanimating]; Start animation

[Oneimageview stopanimating]; Stop animation

Get pictures from the network

UIImage *urlimage = [UIImage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:@ "www.baidu.com"]];

Add to view and free up memory

[Self.view Addsubview:oneimageview];

[Oneimageview release], Oneimageview = nil;

 

How to use Ios--uiimageview

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.