Use of IOS Uiimageview

Source: Internet
Author: User

Two. Two ways to create a Uiimageview 1. Set the picture position and size "' Uiimageview *iv = [[Uiimageview alloc] init]; Created picture, no default width high iv.backgroundcolor = [Uicolor Redcolor]; UIImage *image = [UIImage imagenamed:@ "meinv.jpg"]; Iv.image = image;//Set the picture position and size Iv.frame = CGRectMake (+, image.size.width, image.size.height); [Self.view Addsubview:iv]; " ' 2, use default picture size and location//default width height, for picture width high, position is 0,0 uiimageview *iv = [[Uiimageview alloc]initwithimage:[uiimage imagenamed:@] Meinv.j     PG "]];    It is recommended to set the frame of IV in order to set the position of the picture//iv = CGRectMake (Image.size.width, image.size.height);    Iv.backgroundcolor = [Uicolor Redcolor];    [Self.view Addsubview:iv];    NSLog (@ "%@", Nsstringfromcgrect (Iv.frame)); Iii. contentmode attribute typedef ns_enum (Nsinteger, Uiviewcontentmode) { Uiviewcontentmodescaletofill, Uiviewcontentmodescaleaspectfit,//contents scaled to fit with fixed aspect. Remainder is transparent Uiviewcontentmodescaleaspectfill,//contents scaled to fill with fixed aspect. Some portion of content may ClippEd.              Uiviewcontentmoderedraw,//Redraw on Bounds change (calls-setneedsdisplay) Uiviewcontentmodecenter, Contents remain same size.    Positioned adjusted. Uiviewcontentmodetop, Uiviewcontentmodebottom, Uiviewcontentmodeleft, Uiviewcontentmoderight, UIViewContentMod Etopleft, Uiviewcontentmodetopright, Uiviewcontentmodebottomleft, Uiviewcontentmodebottomright,}; Rules: Any value that contains s Cale the word, the picture will be stretched (scaled) No scale words appear in any value, and the picture will not be stretched Uiviewcontentmodescaletofill, > will stretch the graph according to the uiimageview aspect ratio. Slice > until the entire picture is filled Uiimageview > because it is stretched according to the uiimageview aspect ratio, so the picture will deform law: Any value that contains aspect words will be stretched according to the aspect ratio of the picture &G T     Because it is stretched according to the aspect ratio of the picture, so the picture does not deform uiviewcontentmodescaleaspectfit, > will be stretched according to the aspect ratio of the picture > requires that the entire picture must be within the range of Uiimageview > Width and height One of them must be the same as Uiimageview > center display uiviewcontentmodescaleaspectfill > will stretch according to the aspect ratio of the image > require the entire picture The slice must be filled with Uiimageview > and the width or height of the picture one must be the same as the Uiimageview four. Trim out of partial attribute viewObserve the effect below and understand the Clipstobounds properties//1. Create a uiimageview Uiimageview *iv = [[Uiimageview alloc] init];    2. Set related Properties Iv.backgroundcolor = [Uicolor Redcolor];    Iv.image = [UIImage imagenamed:@ "meinv.jpg"];    Iv.contentmode = Uiviewcontentmodeleft;    3. Set Frame Iv.frame = CGRectMake (100, 100, 200, 200);    Cut out of the part iv.clipstobounds = YES; [Self.view Addsubview:iv]; five. Uiimageview Animation-(ibaction) Run: (UIButton *) sender{nsmutablearray *arrm = [Nsmutablearray ar    Ray];        1. Create more than one picture for (int i = 1; I <= 6; i++) {NSString *imagenmae = [NSString stringwithformat:@ "run_%i", I];        UIImage *image = [UIImage Imagenamed:imagenmae];    2. Place all the images in the array [arrm addobject:image];    }//3. Assigns an array of all pictures saved to uiimageview self.containerView.animationImages = ARRM;     Set the number of repetitions, 0, on behalf of wireless Self.containerView.animationRepeatCount = 1;    Set the time required for an animation self.containerView.animationDuration = 1; Start animation [Self.containerview startanimating]; }-(ibaction) StoP: (ID) Sender {//Determine if animation is being animated if ([Self.containerview isanimating]) {//Stop animation [Self.containerview Stopan    Imating]; } six. Uiimageview performance optimization Problem Description: If you use UIImage *image = [UIImage imagenamed:imagenmae]; load the picture, the picture is automatically cached in memory. At this point, when multiple images are loaded, the animation will cause the memory to burst and will not be released when the animation is finished. Solution://Use Initwithcontentoffile: method to load the image directly from the Mainbundle,app root directory,//So if you encounter the above problem, after the animation, the picture will be automatically released. NSString *imagenmae = [NSString stringwithformat:@ "%@_%i", prefix, i];imagenmae = [[NSBundle Mainbundle] PathForResource : Imagenmae oftype:@ "png"]; UIImage *image = [[UIImage alloc] initwithcontentoffile:imagename]; instead of UIImage *image = [UIImage Imagenamed:imagenmae]; seven. Picture stretching why do I have to stretch the picture? If we want to set a background image, such as a button, when we directly practical pictures (sometimes the art will give us a stretch of the small picture), may be the image of the system stretching deformation, become very ugly, seriously affect the beautiful! Picture stretching is to solve the above problems and exist,    Let the picture in the stretch, ensure that the picture does not deform the picture stretching the historical process:-(void) viewdidload {[Super viewdidload];    UIButton *btn = [[UIButton alloc]init];    Btn.frame = CGRectMake (100, 100, 200, 100);    Old picture UIImage *image = [UIImage imagenamed:@ "common_button_blue_highlighted"]; You can specify a tileOr stretch, get a new picture//Specify protected area 
<div style= "font-family:arial; font-size:14px; " >uiimageresizingmode parameter, used to specify the pattern of stretching: </div><blockquote style= "font-family:arial; font-size:14px; " ><div>uiimageresizingmodestret<wbr>ch: Stretch mode to fill the picture by stretching the rectangular area uiedgeinsets specified </wbr></div> <p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; " >uiimageresizingmodetile: Tile mode, fills the picture </p><p style= "margin-top:0px by repeating the uiedgeinsets specified rectangular area; margin-bottom:0px; padding-top:0px; padding-bottom:0px; " ></p></blockquote><div style= "font-family:arial; font-size:14px; "    The >uiedgeinsets parameter is used to specify the area to be stretched </div> uiedgeinsets insets = Uiedgeinsetsmake (5, 5, 5, 5);    UIImage *newimage = [Image resizableimagewithcapinsets:insets Resizingmode:uiimageresizingmodestretch];    button setting background [btn setbackgroundimage:newimage forstate:uicontrolstatenormal]; [Self.view addsubview:btn];}


Use of 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.