iOS uiimage Zoom

Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">To scale uiimage to a specified size size:</span><br></p></p><p><p>-(UIImage *) scaletosize: (UIImage *) img size: (cgsize) size{</p></p><p><p>Create a bitmap context</p></p><p><p>and set it to the context that is currently being used</p></p><p><p>Uigraphicsbeginimagecontext (size);</p></p><p><p>Draw a picture that changes size</p></p><p><p>[img drawinrect:cgrectmake (0, 0, size.width, size.height)];</p></p><p><p>Create a resized picture from the current context</p></p><p><p>uiimage* scaledimage = Uigraphicsgetimagefromcurrentimagecontext ();</p></p><p><p>Make the current context out of the stack</p></p><p><p>Uigraphicsendimagecontext ();</p></p><p><p>Returns the new resized image</p></p><p><p>Return scaledimage;</p></p><p><p>}</p></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><br></span></p></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><br></span></p></p><p><p>Compress the returned picture according to the size of the picture</p></p><p><p>02+ (UIImage *) fitsmallimage: (UIImage *) image</p></p><p><p>239</p></p><p><p>if (nil = = Image)</p></p><p><p>05 {</p></p><p><p>Return nil;</p></p><p><p>07}</p></p><p><p>If (image.size.width</p></p><p><p>09 {</p></p><p><p>Ten return image;</p></p><p><p>11}</p></p><p><p>Cgsize size = [dsmdataconvert fitsize:image.size];</p></p><p><p>Uigraphicsbeginimagecontext (size);</p></p><p><p>CGRect rect = cgrectmake (0, 0, size.width, size.height);</p></p><p><p>[image drawinrect:rect];</p></p><p><p>UIImage *newing = Uigraphicsgetimagefromcurrentimagecontext ();</p></p><p><p>Uigraphicsendimagecontext ();</p></p><p><p>Return newing;</p></p><p><p>19}</p></p><p><p>+ (cgsize) fitsize: (cgsize) thissize</p></p><p><p>229</p></p><p><p>If (thissize.width = = 0 && thissize.height ==0)</p></p><p><p>Cgsizemake return (0, 0);</p></p><p><p>CGFloat Wscale = thissize.width/image_max_size_width;</p></p><p><p>CGFloat Hscale = thissize.height/image_max_size_geight;</p></p><p><p>CGFloat scale = (wscale>hscale)? wscale:hscale;</p></p><p><p>Cgsize newSize = Cgsizemake (thissize.width/scale, thissize.height/scale);</p></p><p><p>NewSize return;</p></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);">10}</p></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><br></p></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><br></p></p><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><p style="margin-top: 0px; margin-bottom: 5px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; line-height: 21px; color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; white-space: normal; background-color: rgb(226, 226, 226);"><br></p></p><p><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">1. Use the following method to select the image you want to compress, and then output to sentencepic this uiimageview, you call this sentencepic sent to the server on the LINE. </span></span><br><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">2. Set the size of the compression scale is this: Imageframe. You are in the viewdidload, cgrect imageframe = sentencepic.frame; Where Sentencepic.frame is the size of the image you want to Intercept. Self-setting</span></span><br><br><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">-(void) imagepickercontroller: (uiimagepickercontroller *) picker didfinishpickingmediawithinfo: (NSDictionary *) info{</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">    </span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">UIImage *chosenimage = [info objectforkey:uiimagepickercontrollereditedimage];</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">UIImage *sharkenimage = shrinkimage (chosenimage, imageframe.size);</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">sentencepic.image = sharkenimage;</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">    </span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">[picker dismissmodalviewcontrolleranimated:yes];</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">}</span></span><br><br><br><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">static UIImage *shrinkimage (UIImage *original, cgsize Size) {</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgfloat scale = [uiscreen mainscreen].scale;</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb ();</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">    </span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgcontextref context = cgbitmapcontextcreate (nil, size.width *scale, size.height *scale, 8, 0, colorspace, kCGImag Ealphapremultipliedfirst);//null</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgcontextdrawimage (context, cgrectmake (0, 0, size.width *scale, size.height *scale), original. cgimage);</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgimageref shrunken = cgbitmapcontextcreateimage (context);</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">UIImage *final = [UIImage imagewithcgimage:shrunken];</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgcontextrelease (context);</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">cgimagerelease (shrunken);</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">return final;</span></span><br><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);"><span style="word-wrap: normal; word-break: normal; line-height: 21px; font-size: 14px; font-family: ‘Microsoft Yahei‘, simsun, Tahoma; color: rgb(68, 68, 68); background-color: rgb(255, 255, 255);">}</span></span></p><p><p><br></p></p><p><p>iOS uiimage Zoom</p></p></span>
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.