美文网首页
图片裁剪

图片裁剪

作者: 紫wy苏 | 来源:发表于2017-07-07 18:00 被阅读0次

/** TODO:裁剪图片  @param originalImage 原始图片 @param size 目标size @return 预期图片 */

- (UIImage *)thumbnailWithImage:(UIImage *)originalImage size:(CGSize)size{        

CGSize originalsize = [originalImage size];        //原图长宽均小于标准长宽的,不作处理返回原图       

 if (originalsize.widthsize.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();

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;

}

}

相关文章

网友评论

      本文标题:图片裁剪

      本文链接:https://www.haomeiwen.com/subject/uxdrhxtx.html