UIImageView

制作气泡

stretchableImageWithLeftCapWidth

http://blog.csdn.net/justinjing0612/article/details/8751269

1.图片缓存

使用第三方库: SDWebImage

 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
[imageView setImageWithURL:[NSURL URLWithString:@"http://img.taobaocdn.com/bao/uploaded/TB1UXkgGpXXXXcDXVXXSutbFXXX.jpg"]];
[self.view addSubview:imageView];

这样可以实现异步下载, 图片缓存,从过缓存在离线状态下访问图片

2. iOS 图片压缩UIImage

经本人测试:

NSData *data1 = UIImagePNGRepresentation(image);

NSData *data2 = UIImageJPEGRepresentation(image, 1);

NSData *data = UIImageJPEGRepresentation(image, 0.5);

使用以上这三种图片压缩方法, 分别得到了326kb, 121kb, 19kb的图片大小,

图片质量肉眼比较难分辨出来, 但是图片大小相差甚远, 所以一般我们采用第三种, 这里图片质量我定为0.5, 大家可以根据需求在(0 ~ 1)随意改动这个数值

3269470,1212494,192836

iOS自带的提供了一个API如下

  1. NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);

在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。

  1. UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  2. imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];
  3. NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);
  4. m_selectImage = [UIImage imageWithData:imageData];

.h具体code

  1. #import <Foundation/Foundation.h>
  2. @interface UIImage (UIImageExt)
  3. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
  4. - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
  5. @end

.m具体code

    1. #import "UIImageExt.h"
    2. @implementation UIImage (UIImageExt)
    3. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    4. // 创建一个bitmap的context
    5. // 并把它设置成为当前正在使用的context
    6. UIGraphicsBeginImageContext(size);
    7. // 绘制改变大小的图片
    8. [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    9. // 从当前context中创建一个改变大小后的图片
    10. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    11. // 使当前的context出堆栈
    12. UIGraphicsEndImageContext();
    13. // 返回新的改变大小后的图片
    14. return scaledImage;
    15. }
    16. - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
    17. {
    18. UIImage *sourceImage = self;
    19. UIImage *newImage = nil;
    20. CGSize imageSize = sourceImage.size;
    21. CGFloat width = imageSize.width;
    22. CGFloat height = imageSize.height;
    23. CGFloat targetWidth = targetSize.width;
    24. CGFloat targetHeight = targetSize.height;
    25. CGFloat scaleFactor = 0.0;
    26. CGFloat scaledWidth = targetWidth;
    27. CGFloat scaledHeight = targetHeight;
    28. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    29. if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    30. {
    31. CGFloat widthFactor = targetWidth / width;
    32. CGFloat heightFactor = targetHeight / height;
    33. if (widthFactor > heightFactor)
    34. scaleFactor = widthFactor; // scale to fit height
    35. else
    36. scaleFactor = heightFactor; // scale to fit width
    37. scaledWidth  = width * scaleFactor;
    38. scaledHeight = height * scaleFactor;
    39. // center the image
    40. if (widthFactor > heightFactor)
    41. {
    42. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
    43. }
    44. else
    45. if (widthFactor < heightFactor)
    46. {
    47. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    48. }
    49. }
    50. UIGraphicsBeginImageContext(targetSize); // this will crop
    51. CGRect thumbnailRect = CGRectZero;
    52. thumbnailRect.origin = thumbnailPoint;
    53. thumbnailRect.size.width  = scaledWidth;
    54. thumbnailRect.size.height = scaledHeight;
    55. [sourceImage drawInRect:thumbnailRect];
    56. newImage = UIGraphicsGetImageFromCurrentImageContext();
    57. if(newImage == nil)
    58. NSLog(@"could not scale image");
    59. //pop the context to get back to the default
    60. UIGraphicsEndImageContext();
    61. return newImage;
    62. }
    63. @end

UIImage 和 iOS 图片压缩UIImage / UIImageVIew的更多相关文章

  1. iOS 图片压缩方法

    iOS 图片压缩方法 两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size). 压缩图片质量 NSData *data = UIImageJPEGReprese ...

  2. iOS图片压缩上传

    本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  3. iOS图片压缩处理

    理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...

  4. iOS图片压缩

    项目中常会遇到,上传图片的操作,由于iPhone手机直接拍照的图片往往比较大,一般3-4M,如果直接上传不做处理会浪费用户很多流量,再者有很多场景并不需要高清图片,所以在上传图片前对图片进行压缩,是很 ...

  5. iOS 图片压缩

    + (UIImage *)scaleFromImage:(UIImage *)image {    CGSize newSize = CGSizeMake(366, 366);   //目标图片的大小 ...

  6. iOS图片压缩问题

    对于压缩的处理我给出的建议是 先判断 图片的大小,如果是本地图片最好用nsfilemanager 来判断 .如果不能用这个判断的话 就只能先将图片转成data,然后再判断了. 图片转成data 当然就 ...

  7. iOS 图片剪切和压缩的几个方法

    // 图片剪切 - (UIImage*)clipImageWithImage:(UIImage*)image inRect:(CGRect)rect {    CGImageRef imageRef ...

  8. iOS UIImage:获取图片主色调

    本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...

  9. iOS GIF图片转UIImage

    多平台保持统一风格的UI设计,少不了一些动态图片的使用 1.本地GIF图片使用 1.1 将本地GIF转为NSdata类型 NSData *tempdata = [NSData dataWithCont ...

随机推荐

  1. ubuntu安装 laravel 过程中出现: mcrypt php extension required 的问题 | 以及composer相关问题 | Nginx安装

    这篇文章对于Nginx的配置至关重要 如果碰到访问index.php不返回html而出现下载文件的问题,加上那段default就可以修正: https://www.digitalocean.com/c ...

  2. Javascript基础系列之(三)数据类型 (数值 Number)

    javascript中想限定一个数的数值,无需限定它是整数还是浮点数型 var num1 = 80 ; var num2 = 55.51; var num3 = -34; var num4 = 9e5 ...

  3. 【Moqui业务逻辑翻译系列】Story of Online Retail Company 在线零售公司的故事

    h1. Story of Online Retail Company 在线零售公司的故事 Someone decides to sell a product. [Product Marketer Ma ...

  4. 【团队项目演示】FZU5BOYS之团队项目链接汇总

    FZU5BOYS      项目冲刺之博客汇总 Alpha版本 Day One Day Two Day Three Day Four Day Five Day Six Day Seven Day Ei ...

  5. UVA 540 stl

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  6. Oracle 11g 默认用户名和密码

    安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下: 用户名 / 密码                      登录身份                              说明 ...

  7. PLSQL导入Excel表中数据

     PL/SQL 和SQL Sever导入excel数据的原理类似,就是找到一个导入excel数据的功能项,按照步骤走就是了.下面是一个些细节过程,希望对像我这样的菜鸟有帮助.  www.2cto.co ...

  8. mysql prepare语句使用

    语法 PREPARE statement_name FROM sql_text /*定义*/ EXECUTE statement_name [USING variable [,variable...] ...

  9. java变量作用域

      1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可 ...

  10. poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))

    这里不在详细介绍威佐夫博弈论 简单提一下 要先提出一个名词“奇异局势”,如果你面对奇异局势则必输 奇异局势前几项(0,0).(1,2).(3,5).(4,7).(6,10).(8,13).(9,15) ...