IOS modifies Uiimageview Frame based on UIImage (including the middle portion of the captured Image)

Source: Internet
Author: User
<span id="Label3"></p><p><p>IOS Uiimageview Adjust frame as needed</p></p><p><p>1, the width and height of the picture is unequal, the middle part of the image is intercepted, the part size is clear</p></p><p><p>2. The width of the picture is equal to the width of the class of its parent view, then the height is calculated according to the width, guaranteeing</p></p><p><p>Picture does not deform, display normal</p></p><p><p>3, the width or height of the picture is greater than its parent view of the class width or height, if the problem is width,</p></p><p><p>Determines the display width of the image, whichever is the width of the parent view, and if it is a height problem,</p></p><p><p>The height of the parent view, whichever is higher, determines the display height of the Picture.</p></p><p><p>In the above three cases, the specific examples are as Follows:</p></p><p><p></p></p><p><p>In the first case, the method is Called:</p></p><p><p>-(void) imageloadedfinish: (urlimageview *) oldimageview image: (UIImage *) Imagev skuinfo: (nsdictionary *) skuinfo</p></p><p><p>{<br>Target size of interception</p></p><p><p>FLOATIMGW = (win_size.width-40)/3;</p></p><p><p>Intercept the middle part</p></p><p><p>Oldimageview.image =[self Squareimagefromimage:imagev scaledtosize:imgw];</p></p><p><p>}</p></p><p><p></p></p><p><p>---operation//intercept Middle Part</p></p><p><p>-(UIImage *) squareimagefromimage: (uiimage*) image scaledtosize: (cgfloat) newSize</p></p><p><p>{</p></p><p><p>--zoom ratio</p></p><p><p>Cgaffinetransform scaletransform;</p></p><p><p>Cgpoint origin;</p></p><p><p>Picture size is larger than picture height</p></p><p><p>If (image.size.width >image.size.height)</p></p><p><p>{</p></p><p><p>Image has an original height of 200, and the height of the scaled image is 400pixels, so the zoom ratio is 2</p></p><p><p></p></p><p><p>Calculating the zoom ratio</p></p><p><p>CGFloat scaleratio = newsize/image.size.height;</p></p><p><p>ScaleTransform = Cgaffinetransformmakescale (scaleratio, scaleratio);</p></p><p><p>Sets the brush coordinates for drawing the original picture to Cgpoint ( -100, 0) pixels</p></p><p><p>Calculate the x-axis of a brush</p></p><p><p>Origin = Cgpointmake (-(image.size.width-image.size.height)/2.0f, 0);</p></p><p><p>} else</p></p><p><p>{</p></p><p><p>Picture height is greater than the width of the case</p></p><p><p>CGFloat scaleratio = newsize/image.size.width;</p></p><p><p>ScaleTransform = Cgaffinetransformmakescale (scaleratio, scaleratio);</p></p><p><p>Calculate the y-axis of a brush</p></p><p><p>Origin = Cgpointmake (0,-(image.size.height-image.size.width)/2.0f);</p></p><p><p>}</p></p><p><p></p></p><p><p>Cgsize size = Cgsizemake (newSize, newSize);</p></p><p><p>Create Artboards as (400x400) pixels</p></p><p><p>If ([[uiscreen mainscreen] respondstoselector: @selector (scale)])</p></p><p><p>{</p></p><p><p>Uigraphicsbeginimagecontextwithoptions (size, YES, 0);</p></p><p><p>} else</p></p><p><p>{</p></p><p><p>Uigraphicsbeginimagecontext (size);</p></p><p><p>}</p></p><p><p></p></p><p><p>Cgcontextref context = Uigraphicsgetcurrentcontext ();</p></p><p><p>Zooms the image original picture (400x200) pixels to (800x400) pixels</p></p><p><p>CGCONTEXTCONCATCTM (context, scaletransform);</p></p><p><p>Origin is also scaled from primitive (-100, 0) to (-200, 0)</p></p><p><p>[image drawatpoint:origin];</p></p><p><p></p></p><p><p>Gets the image image that is clipped after zooming</p></p><p><p>Image = Uigraphicsgetimagefromcurrentimagecontext ();</p></p><p><p>End Artboard Drawing</p></p><p><p>Uigraphicsendimagecontext ();</p></p><p><p></p></p><p><p>Return image;</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>Second Case:</p></p><p><p>Equal scale Scaling</p></p>-(void) imageloadedfinish: (urlimageview *) oldimageview image: (UIImage *) Imagev skuinfo: (nsdictionary *) skuinfo<p><p>{the frame that gets the current view</p></p><p><p>Cgsize selfsize = self.frame.size;</p></p><p><p>Get the frame of the image</p></p><p><p>Cgsize imageSize = imagev.size;</p></p><p><p>Get the ImageView frame</p></p><p><p>CGRect Imagevrect = self.imageView.frame;</p></p><p><p>Determine the width of the ImageView</p></p><p><p>ImageVRect.size.width = selfsize.width;</p></p><p><p>Calculates the height of a imageview based on its width</p></p><p><p>ImageVRect.size.height = imagevrect.size.width*imagesize.height/imagesize.width;</p></p><p><p>Calculate x, y</p></p><p><p>imagevrect.origin.x = 0;</p></p><p><p>IMAGEVRECT.ORIGIN.Y = 0;</p></p><p><p>Self.imageView.frame = imagevrect;</p></p><p><p>CGRect Selfrect = self.frame;</p></p><p><p>SelfRect.size.width = imageVRect.size.width;</p></p><p><p>SelfRect.size.height = imageVRect.size.height;</p></p><p><p>Self.frame = selfrect;</p></p><p><p>}</p></p><p><p></p></p><p><p>The third case:</p></p><p><p>Equal scale Scaling</p></p><p><p>(void) imageloadedfinish: (urlimageview *) oldimageview image: (UIImage *) Imagev skuinfo: (nsdictionary *) skuinfo</p></p><p><p>{</p></p><p><p>Frame that gets the current view</p></p><p><p>Cgsize selfsize = self.frame.size;</p></p><p><p>Gets the frame of the current image</p></p><p><p>Cgsize imageSize = imagev.size;</p></p><p><p>Get the current ImageView frame</p></p><p><p>CGRect Imagevrect = self.imageView.frame;</p></p><p><p>The width of the image is greater than the width of the current view</p></p><p><p>If (imagesize.width > Selfsize.width)</p></p><p><p>{</p></p><p><p>Calculate height according to width, determine width</p></p><p><p>ImageVRect.size.height = Selfsize.width * imagesize.height/imagesize.width;</p></p><p><p>ImageVRect.size.width = selfsize.width;</p></p><p><p>}</p></p><p><p>The height of the image is greater than the height of the current view</p></p><p><p>If (imageVRect.size.height > Selfsize.height)</p></p><p><p>{</p></p><p><p></p></p><p><p>Determines width based on height calculation width</p></p><p><p>ImageVRect.size.width = Selfsize.height * imagevrect.size.width/imagevrect.size.height;</p></p><p><p>ImageVRect.size.height = selfsize.height;</p></p><p><p>}</p></p><p><p></p></p><p><p>Calculate x, y</p></p><p><p>imagevrect.origin.x = (selfsize.width-imagevrect.size.width)/2;</p></p><p><p>IMAGEVRECT.ORIGIN.Y = (selfsize.height-imagevrect.size.height)/2;</p></p><p><p>Self.imageView.frame = imagevrect;</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>IOS modifies Uiimageview Frame based on UIImage (including the middle portion of the captured Image)</p></p></span>

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.