取图片相关 我有个一张图片200x200现在想把它缩小到100x100然后返回UIImage接口。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 @implementation UIImage (Extras)#pragma -#pragma Scale and crop image- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize{UIImage *sourceImage = self;UIImage *newImage = nil; CGSize imageSize = sourceImage.size;CGFloat width = imageSize.width;CGFloat height = imageSize.height;CGFloat targetWidth = targetSize.width;CGFloat targetHeight = targetSize.height;CGFloat scaleFactor = 0.0;CGFloat scaledWidth = targetWidth;CGFloat scaledHeight = targetHeight;CGPoint thumbnailPoint = CGPointMake(0.0,0.0);if (CGSizeEqualToSize(imageSize, targetSize) == NO) { CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if (widthFactor > heightFactor) scaleFactor = widthFactor; // scale to fit height else scaleFactor = heightFactor; // scale to fit width scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; // center the image if (widthFactor > heightFactor) { thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; } else if (widthFactor < heightFactor) { thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(targetSize); // this will cropCGRect thumbnailRect = CGRectZero;thumbnailRect.origin = thumbnailPoint;thumbnailRect.size.width = scaledWidth;thumbnailRect.size.height = scaledHeight;[sourceImage drawInRect:thumbnailRect];newImage = UIGraphicsGetImageFromCurrentImageContext();if(newImage == nil) NSLog(@"could not scale image");//pop the context to get back to the defaultUIGraphicsEndImageContext();return newImage;} Mac下网络架构设计软件 关于Objective-C中所谓的分类 想改变MKAnnotationView弹出的注释 求助 如何初始化一个文本框的内容,让其默认显示一些文字 导航栏隐藏展开效果 ios openssl rsa 加密问题 ios在中国发展怎么样 x-code 5.1 zxing使用的问题 ios8.1.2能降级到ios7吗? app和safari之间跳转问题 http下载文件问题 如何获取http下载速度 关于webview的缓存
#pragma Scale and crop image- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height; if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor; // center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
} UIGraphicsBeginImageContext(targetSize); // this will cropCGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;[sourceImage drawInRect:thumbnailRect];newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image");//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}