ios中图像进行压缩方法汇总

2016-02-19 11:31 6 1 收藏

get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天图老师小编跟大家分享的是ios中图像进行压缩方法汇总,一起来学习了解下吧!

【 tulaoshi.com - 编程语言 】

方法一:

代码如下:

- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
 CGSize imageSize = image.size;
 CGFloat width = imageSize.width;
 CGFloat height = imageSize.height;
     
 if (width = newSize.width && height = newSize.height){
  return image;
 }
     
 if (width == 0 || height == 0){
  return image;
 }
     
 CGFloat widthFactor = newSize.width / width;
 CGFloat heightFactor = newSize.height / height;
 CGFloat scaleFactor = (widthFactorheightFactor?widthFactor:heightFactor);
     
 CGFloat scaledWidth = width * scaleFactor;
 CGFloat scaledHeight = height * scaleFactor;
 CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);
     
    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

方法二:

.h具体code
代码如下:

#import Foundation/Foundation.h 
@interface UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size; 
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize; 
@end 

.m具体code

代码如下:

#import "UIImageExt.h" 
@implementation UIImage (UIImageExt) 
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ 
    // 创建一个bitmap的context 
    // 并把它设置成为当前正在使用的context 
    UIGraphicsBeginImageContext(size); 
    // 绘制改变大小的图片 
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    // 从当前context中创建一个改变大小后的图片 
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    // 使当前的context出堆栈 
    UIGraphicsEndImageContext(); 
    // 返回新的改变大小后的图片 
    return scaledImage; 

- (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 crop 
    CGRect 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; 

@end 

方法三:(本人项目中使用的方法)

代码如下:

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

以上所述就是本文的全部内容了,希望大家能够喜欢。

来源:https://www.tulaoshi.com/n/20160219/1597758.html

延伸阅读
360压缩如何进行固实压缩,什么是固实压缩?   固实压缩是一种特殊的压缩存储格式,固实压缩把要压缩的全部文件当做一个连续的数据流来处理。通俗的可以这样认为:普通压缩是把一个文件一个文件分别压缩然后合成压缩包,固实压缩是先把这些文件连接起来当做一个大文件进行压缩。这样的优点是在压缩超大量小体积文件时压缩率更高,压缩...
360压缩如何对压缩包进行木马检测?   用360压缩打开一个压缩包后,会自动对压缩包内的文件进行云木马检测,保证使用安全。经过云木马检测后,360压缩会对压缩包内不同类型的文件进行安全评级,有效识别木马、脚本、普通文件、风险文件,并在界面右上角会有相应的图片提示。需要说明的是:在安全级别为木马的文件上双击文件会弹出阻...
ios7冷门技巧汇总   1)Siri支持推文搜索 Siri现在支持搜索Twitter推文。 2)Siri支持男声 tuLaoShi.com你可以将Siri的声音切换成男声:设置通用Siri声音性别。 3)屏蔽特定手机号或联系人(www.tulaoshi.com) 你可以屏蔽来自特定手机号码的来电或者短信:设置手机屏蔽将号码或者通讯录里的联系人添加至屏...
第一种方法:使用UIView and UIActivityIndicatorView 代码如下: //创建UIWebView WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)]; [WebView setUserInteractionEnabled:NO]; [WebView setBackgroundColor:[UIColor clearColor]]; [WebView setDelegate:self]; [WebView setOpaque:NO];//使网页透明 NSString *p...
在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理。 例如: //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //设定时间格式,这里可以设置成自己需要的格式 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //用[NSDate d...

经验教程

940

收藏

35
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部