#pragma mark- 缩放图片按比例
+ (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
#pragma mark- 缩放图片按大小
+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)scaleSize
{
UIGraphicsBeginImageContext(scaleSize);
[image drawInRect:CGRectMake(0, 0, scaleSize.width, scaleSize.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
#pragma mark- 缩放图片按大小,同时保持比例
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{
UIImage *scaleImage;
CGSize newSize = CGSizeMake(96.0, 96.0);
if (nil == image) {
scaleImage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (newSize.width/newSize.height > oldsize.width/oldsize.height) {
rect.size.width = newSize.height*oldsize.width/oldsize.height;
rect.size.height = newSize.height;
rect.origin.x = (newSize.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
rect.size.width = newSize.width;
rect.size.height = newSize.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (newSize.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(newSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, newSize.width, newSize.height));//clear background
[image drawInRect:rect];
scaleImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return scaleImage;
}
+ (BOOL)checkImageValid:(UIImage *)imageObj
{
if (imageObj && imageObj.size.height > 0 && imageObj.size.width > 0)
{
return YES;
}
return NO;
}
IOS开发笔记,对图像的缩放操作
最新推荐文章于 2023-08-16 15:42:40 发布