在IOS中将大图像转换为小图像
我正在开发一个应用程序,该程序必须将从相机拍摄的图像加载到tableview中,通常约为5-6MB。我正在异步加载图像并缓存它,但我想知道是否有任何方法可以将这些大尺寸图像更改为较小尺寸 试试这个简单的方法。。。我从这篇文章中发现在IOS中将大图像转换为小图像,ios,iphone,objective-c,uitableview,uiimage,Ios,Iphone,Objective C,Uitableview,Uiimage,我正在开发一个应用程序,该程序必须将从相机拍摄的图像加载到tableview中,通常约为5-6MB。我正在异步加载图像并缓存它,但我想知道是否有任何方法可以将这些大尺寸图像更改为较小尺寸 试试这个简单的方法。。。我从这篇文章中发现 在这里,我把裁剪和调整图像大小的逻辑,使用它根据您的要求 对于被裁剪的图像: UIImage *croppedImg = nil; CGRect cropRect = CGRectMake(AS YOu Need); // set frame as you need
在这里,我把裁剪和调整图像大小的逻辑,使用它根据您的要求 对于被裁剪的图像:
UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need); // set frame as you need
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
以下方法返回UIImage
(根据您需要的图像大小)
在这里,您可以得到通过上述方法返回的交叉图像
或调整大小
也可以使用以下方法对图像的特定高和宽进行调整大小:
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(CGFloat)width withHeight:(CGFloat)height
{
CGSize newSize = CGSizeMake(width, height);
CGFloat widthRatio = newSize.width/image.size.width;
CGFloat heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
此方法返回新图像,并带有所需的特定大小
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(CGFloat)width withHeight:(CGFloat)height
{
CGSize newSize = CGSizeMake(width, height);
CGFloat widthRatio = newSize.width/image.size.width;
CGFloat heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}