First, Uiimageview and UIImage
UIImageViewis the class used in iOS 显示图片 , UIImage is the class used, and 存储图片数据 almost all of the picture data seen in iOS is stored in, and the UIImage desired images are UIImageView displayed; Uiimageview and uiimage relationships such as:
Two. Two ways to create a Uiimageview 1. Set the image position and size yourself
"'
Uiimageview *iv = [[Uiimageview alloc] init]; Created picture, no default width height
Iv.backgroundcolor = [Uicolor Redcolor];
UIImage *image = [UIImage imagenamed:@ "meinv.jpg"];
Iv.image = image;
//自己设置图片位置和尺寸iv.frame = CGRectMake(100, 100, image.size.width, image.size.height);[self.view addSubview:iv];
"'
2. Use default picture size and location
//默认宽高,为图片宽高,位置为0,0 [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"meinv.jpg"]]; //推荐设置iv的frame,以便于设置图片的摆放位置 //iv = CGRectMake(100100, image.size.width, image.size.height); iv.backgroundColor = [UIColor redColor]; [self.view addSubview:iv]; NSLog(@"%@",NSStringFromCGRect(iv.frame));
Three. Contentmode Properties
typedefNs_enum (Nsinteger, Uiviewcontentmode) {Uiviewcontentmodescaletofill, Uiviewcontentmodescaleaspectfit,//Contents scaled to fit with fixed aspect. Remainder is transparentUiviewcontentmodescaleaspectfill,//Contents scaled to fill with fixed aspect. Some portion of content may be 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 followUiimageviewAspect ratio to stretch the picture > until the entire picture is filledUiimageviewSo far > because it is in accordance withUiimageviewThe aspect ratio to stretch, so the picture will deform the law: Any value that contains aspect words, will be in accordance with the picture of the aspect ratio to stretch > because it is according to the picture of the aspect ratio to stretch, so the picture will not be deformed uiviewcontentmodescaleaspect Fit, > stretches the image by its aspect ratio > requires the entire picture to beUiimageviewWithin the range of > and width and height of which one must andUiimageviewSame as > Center display Uiviewcontentmodescaleaspectfill, > stretches according to the aspect ratio of the picture > requires that the entire picture must be populatedUiimageview> and width or height of the picture one must andUiimageviewThe same
Four. Trim out-of-section properties
Observe the following running effect and understand the Clipstobounds property//1. Create a Uiimageview Uiimageview*iv = [[UiimageviewALLOC] init];//2. Setting related propertiesiv. BackgroundColor= [UicolorRedcolor]; iv. Image= [UIImageimagenamed:@"Meinv.jpg"]; iv. Contentmode= Uiviewcontentmodeleft;//3. Set Frameiv. Frame= CGRectMake ( -, -, $, $);//Cut out of the sectioniv. Clipstobounds=YES; [ Self. ViewADDSUBVIEW:IV];
Five. Uiimageview Animation
- (ibaction) Run: (UIButton*) sender{Nsmutablearray*ARRM = [NsmutablearrayArray];//1. Create more than one picture for(inti =1; I <=6; i++) {NSString*imagenmae = [NSStringstringwithformat:@"Run_%i", I];UIImage*image = [UIImageImagenamed:imagenmae];//2. Put all the pictures in the array[Arrm Addobject:image]; }//3. Assign an array of all pictures saved to Uiimageview Self. Containerview. Animationimages= ARRM;//Set number of repetitions, 0, on behalf of the wireless Self. Containerview. Animationrepeatcount=1;//Set the time required to animate one time Self. Containerview. Animationduration=1;//Start animation[ Self. ContainerviewStartanimating]; }- (ibaction) Stop: (ID) Sender {//Determine if the animation is in progress if([ Self. ContainerviewIsanimating]) {//Stop animation[ Self. ContainerviewStopanimating]; }}
Six. Uiimageview Performance optimization Issues
Problem Description: If you use UIImage *image = [UIImage imagenamed:imagenmae]; load the picture, the picture will be 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.
解决方案://使用initWithContentOfFile:方法直接从mainBundle,app根目录中加载图片,//这样如果遇到上述问题,在执行完动画之后,图片会自动释放..NSString *imageNmae = [NSString stringWithFormat:@"%@_%i", prefix, i];imageNmae = [[NSBundle mainBundle] pathForResource:imageNmae ofType:@"png"];UIImage *image = [[UIImage alloc] initWithContentOfFile:imageName];代替UIImage *image = [UIImage imageNamed:imageNmae];
Seven. Picture stretching
Why do you want to stretch the picture?
If we want to set a background image, such as a button, when we direct practical pictures (sometimes the art will give us a stretch of small pictures), may be the image of the system stretching deformation, become very ugly, seriously affect the beautiful!
Picture stretching is to solve the above problems exist, let the picture in the stretch, to ensure that the picture is not deformed
Picture Stretching history Process:
- (void) Viewdidload {[SuperViewdidload];UIButton*BTN = [[UIButtonAlloc]init]; Btn. Frame= CGRectMake ( -, -, $, -);//Old pictures UIImage*image = [UIImageimagenamed:@"common_button_blue_highlighted"];//You can specify a tile or stretch to get a new picture //Specify a protected area uiedgeinsetsinsets = Uiedgeinsetsmake (5,5,5,5);UIImage*newimage = [Image resizableimagewithcapinsets:insets Resizingmode:uiimageresizingmodestretch];//button setting background[Btn Setbackgroundimage:newimage Forstate:uicontrolstatenormal]; [ Self. ViewADDSUBVIEW:BTN];}
For the picture processing there are many, we can search the Internet, here to recommend an article:
http://blog.csdn.net/hastar521/article/details/49122607
IOS UI Base Control Uiimageview