[IPhone] How to narrow down uiimage

Source: Internet
Author: User

This Code demonstrates how to narrow down the uiimage

 

@ Implementation uiimage (extras) <br/> # pragma mark-<br/> # pragma mark scale and crop image <br/>-(uiimage *) imagebyscalingandcroppingforsize :( cgsize) targetsize <br/> {<br/> uiimage * sourceimage = self; <br/> uiimage * newimage = nil; <br/> cgsize imagesize = sourceimage. size; <br/> cgfloat width = imagesize. width; <br/> cgfloat Height = imagesize. height; <br/> cgfloat targetwidth = targetsize. width; <br/> cgfloat targetheight = targetsize. height; <br/> cgfloat scalefactor = 0.0; <br/> cgfloat scaledwidth = targetwidth; <br/> cgfloat scaledheight = targetheight; <br/> cgpoint thumbnailpoint = cgpointmake (0.0, 0.0); <br/> If (cgsizeequaltosize (imagesize, targetsize) = No) <br/>{< br/> cgfloat widthfactor = targetwidth/width; <br/> cgfloat heightfactor = targetheight/height; <br/> If (widthfactor> heightfactor) <br/> scalefactor = widthfactor; // scale to fit height <br/> else <br/> scalefactor = heightfactor; // scale to fit width <br/> scaledwidth = width * scalefactor; <br/> scaledheight = height * scalefactor; <br/> // center the image <br/> If (widthfactor> heightfactor) <br/> {<br/> thumbnailpoint. y = (targetheight-scaledheight) * 0.5; <br/>}< br/> else <br/> If (widthfactor <peightfactor) <br/>{< br/> thumbnailpoint. X = (targetwidth-scaledwidth) * 0.5; <br/>}< br/> uigraphicsbeginimagecontext (targetsize ); // This will crop <br/> cgrect thumbnailrect = cgrectzero; <br/> thumbnailrect. origin = thumbnailpoint; <br/> thumbnailrect. size. width = scaledwidth; <br/> thumbnailrect. size. height = scaledheight; <br/> [sourceimage drawinrect: thumbnailrect]; <br/> newimage = uigraphicsgetimagefromcurrentimagecontext (); <br/> If (newimage = nil) <br/> nslog (@ "cocould not scale image"); <br/> // pop the context to get back to the default <br/> uigraphicsendimagecontext (); <br/> return newimage; <br/>}< br/>

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.