A common way to crop pictures

Source: Internet
Author: User

Reproduced in this article to http://www.cocoachina.com/bbs/read.php?tid=177476 this code is the main function of the size or proportion of the image to cut the middle part, or proportionally narrow, there may be insufficient parts, please Daniel:

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384 + (UIImage *)handleImage:(UIImage *)originalImage withSize:(CGSize)size{    CGSize originalsize = [originalImage size];    NSLog(@"改变前图片的宽度为%f,图片的高度为%f",originalsize.width,originalsize.height);        //原图长宽均小于标准长宽的,不作处理返回原图    if (originalsize.width<size.width && originalsize.height<size.height)    {        return originalImage;    }        //原图长宽均大于标准长宽的,按比例缩小至最大适应值    else if(originalsize.width>size.width && originalsize.height>size.height)    {        CGFloat rate = 1.0;        CGFloat widthRate = originalsize.width/size.width;        CGFloat heightRate = originalsize.height/size.height;                rate = widthRate>heightRate?heightRate:widthRate;                CGImageRef imageRef = nil;                 if (heightRate>widthRate)        {            imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2-size.height*rate/2, originalsize.width, size.height*rate));//获取图片整体部分        }        else        {            imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2-size.width*rate/2, 0, size.width*rate, originalsize.height));//获取图片整体部分        }        UIGraphicsBeginImageContext(size);//指定要绘画图片的大小        CGContextRef con = UIGraphicsGetCurrentContext();                CGContextTranslateCTM(con, 0.0, size.height);        CGContextScaleCTM(con, 1.0, -1.0);                CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);                UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();        NSLog(@"改变后图片的宽度为%f,图片的高度为%f",[standardImage size].width,[standardImage size].height);                UIGraphicsEndImageContext();        CGImageRelease(imageRef);                return standardImage;    }        //原图长宽有一项大于标准长宽的,对大于标准的那一项进行裁剪,另一项保持不变    else if(originalsize.height>size.height || originalsize.width>size.width)    {        CGImageRef imageRef = nil;                if(originalsize.height>size.height)        {            imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2-size.height/2, originalsize.width, size.height));//获取图片整体部分        }        else if (originalsize.width>size.width)        {            imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2-size.width/2, 0, size.width, originalsize.height));//获取图片整体部分        }                UIGraphicsBeginImageContext(size);//指定要绘画图片的大小        CGContextRef con = UIGraphicsGetCurrentContext();                CGContextTranslateCTM(con, 0.0, size.height);        CGContextScaleCTM(con, 1.0, -1.0);                CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);                 UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();        NSLog(@"改变后图片的宽度为%f,图片的高度为%f",[standardImage size].width,[standardImage size].height);                UIGraphicsEndImageContext();        CGImageRelease(imageRef);                return standardImage;    }        //原图为标准长宽的,不做处理    else    {        return originalImage;    }}

A common way to crop pictures

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.