IOS image to achieve the process of stretching without distortion _ios

Source: Internet
Author: User
Tags uikit

In the actual development of iOS, if we put a picture (a special shape, especially a picture with a rounded corner) in the UIButton, when the UIButton changes size, the picture may be stretched and deformed, and we can pass -(UIImage *) Resizableimagewithcapinsets:resizingmode: method (Calling the method through the UIImage object, and passing in the name of the picture to be stretched as a parameter) implements a picture that can be stretched without distortion, Here we write this method into the classification of the UIImage class to encapsulate it, and we can use it directly in future iOS development:

Uiimage+extension.h

#import <UIKit/UIKit.h>

@interface uiimage (Extension)
/**
 * The name of the incoming picture, returning a picture that can be stretched without distortion
 *
 * param imagename Picture Name
 *
 * @return stretch picture/
+ (UIImage *) Resizableimagewithname: (NSString *) imagename;
@end

Uiimage+extension.m

#import "Uiimage+extension.h"

@implementation uiimage (Extension)

+ (UIImage *) Resizableimagewithname: ( NSString *) imagename
{

  //loading original picture
  uiimage *norimage = [UIImage imagenamed:imagename];
  Get the width of the original picture of half
  cgfloat w = norImage.size.width * 0.5;
  CGFloat h = norImage.size.height * 0.5;
  Generates a picture that can stretch the specified location
  uiimage *newimage = [Norimage Resizableimagewithcapinsets:uiedgeinsetsmake (H, W, H, W) Resizingmode:uiimageresizingmodestretch];

  return newimage;
}
@end

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.