"Learning iOS: UI series" Modifying the size of a picture operation

Source: Internet
Author: User

1. Add picture operation with UIImage object when adding picture operation

1.[uiimage imagename:@ "1.png"]

2.[uiimage imagewithcontentsoffile:@ "1.png"]

Note: The difference between imagenamed and Imagewithcontentsoffile

Imagenamed loads the image and caches it, and caches the image in memory, using imagenamed when the image is small and frequently used;
Imagewithcontentsoffile is to display only pictures, but not in memory. So when loading a lot of pictures with imagewithcontentsoffile better, the memory will not become larger.

when adding a picture operation, first modify the image size, as follows :

Note: This is the category extension class for UIImage

①uiimage+scale.h declaration file, passing in a cgsize parameter, that is, the width and height of the picture

<span style= "FONT-SIZE:14PX;" >-(UIImage *) Scaletosize: (cgsize) size;</span>
②.m files, implementing Files
<span style= "FONT-SIZE:14PX;" >  //Specify reduced size-(UIImage *) Scaletosize: (cgsize) Size {    //Create a bitmap context    //and set it as the currently in use context    uigraphicsbeginimagecontext (size);    Draw a picture that changes size    [self drawinrect:cgrectmake (0,0, Size.width, Size.Height)];    Creates a resized image from the current context    UIImage * scaledimage = Uigraphicsgetimagefromcurrentimagecontext ();    Make the current context out of the stack    uigraphicsendimagecontext ();    Returns the new resized picture return    scaledimage;} </span>
method calls, as follows.
<span style= "FONT-SIZE:14PX;" >uiimage *image = [UIImage imagewithcontentsoffile:@ "1.png"];image = [Image Scaletosize:cgsizemake (30, 30)]; Uiimageview *imgeview = [[Uiimageview alloc] Initwithimage:image];</span>


"Learning iOS: UI series" Modifying the size of a picture operation

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.