Controller:
////VIEWCONTROLLER.M//12-Drawing problems with pictures////Created by Xiaomage on 15/12/30.//2015 little brother. All rights reserved.//#import "ViewController.h"#import "Uiimage+xmgextention.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UIButton*button;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1.1 Creating a UIImage objectUIImage *image = [UIImage resizableimagewithlocalimagename:@"Car"];//methods for extending the system classes//UIImage *image = [UIImage imagenamed:@ "Car"]; //1.2 Get the size of the image /*cgfloat imagewidth = image.size.width; CGFloat imageheight = image.size.height; */ //1.3 Returns a protected and stretched picture--->capinsets: which places to protect: the height of the protection of the picture half, the left to protect the width of the picture half, the right to protect the width of half minus one, below the protection height of half minus one. Only the area of the middle 1*1 is stretched. //Way One /*UIImage *resizableimage = [Image resizableimagewithcapinsets:uiedgeinsetsmake (ImageHeight * 0.5, ImageWidth * 0.5, ImageHeight * 0.5-1, ImageWidth * 0.5-1)]; Uiimageresizingmodetile, tiling uiimageresizingmodestretch, stretching (scaling) UIImage *resizableimage = [Image Resizableimagewi Thcapinsets:uiedgeinsetsmake (ImageHeight * 0.5, ImageWidth * 0.5, ImageHeight * 0.5-1, ImageWidth * 0.5-1) ResizingMode : Uiimageresizingmodetile]; */ //Mode two /*///right area to be protected = width-leftcapwidth-1 of picture//Bottom cap = height-topcapheight-1 UIImage *resizableimag e = [Image stretchableimagewithleftcapwidth:imagewidth * 0.5 topcapheight:imageheight * 0.5]; */ //2. Set the picture to the button[Self.button setbackgroundimage:image forstate:uicontrolstatenormal]; }@end
UIImage Extensions, Categories:
// // uiimage+xmgextention.h// 12-picture stretching problem //#import <uikit/ uikit.h>@interface UIImage (xmgextention) // category / * */+ (Instancetype) Resizableimagewithlocalimagename: (NSString *) localimagename; @end
////uiimage+xmgextention.m//#import "Uiimage+xmgextention.h"@implementationUIImage (xmgextention)+ (Instancetype) Resizableimagewithlocalimagename: (NSString *) localimagename{//Create a Picture objectUIImage *image =[UIImage Imagenamed:localimagename]; //get the dimensions of a pictureCGFloat imagewidth =Image.size.width; CGFloat imageheiht=Image.size.height; //returns an extruded and protected picture return[Image Stretchableimagewithleftcapwidth:imagewidth *0.5TOPCAPHEIGHT:IMAGEHEIHT *0.5 ];}@end
IOS25---picture stretching