ios中圖像進行壓縮方法匯總
更新時間:2015年05月27日 11:24:49 投稿:hebedich
在Iphone上有兩種讀取圖片數(shù)據(jù)的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數(shù)需要兩個參數(shù):圖片的引用和壓縮系數(shù).而UIImagePNGRepresentation只需要圖片引用作為參數(shù).
方法一:
復(fù)制代碼 代碼如下:
- (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 = (widthFactor<heightFactor?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;
}
方法二:
.h具體code
復(fù)制代碼 代碼如下:
#import <Foundation/Foundation.h>
@interface UIImage (UIImageExt)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
.m具體code
復(fù)制代碼 代碼如下:
#import "UIImageExt.h"
@implementation UIImage (UIImageExt)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 創(chuàng)建一個bitmap的context
// 并把它設(shè)置成為當(dāng)前正在使用的context
UIGraphicsBeginImageContext(size);
// 繪制改變大小的圖片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 從當(dāng)前context中創(chuàng)建一個改變大小后的圖片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使當(dāng)前的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
方法三:(本人項目中使用的方法)
復(fù)制代碼 代碼如下:
-(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;
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- iOS中時間與時間戳的相互轉(zhuǎn)化實例代碼
- IOS 時間和時間戳之間轉(zhuǎn)化示例
- iOS獲取當(dāng)前時間和當(dāng)前時間戳的方法
- iOS開發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
- iOS長按UIlabel實現(xiàn)可復(fù)制功能
- iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
- iOS應(yīng)用開發(fā)中UIView添加邊框顏色及設(shè)置圓角邊框的方法
- iOS實現(xiàn)壓縮圖片上傳功能
- iOS常用小功能(獲得屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size)
相關(guān)文章
iOS應(yīng)用運用設(shè)計模式中的Strategy策略模式的開發(fā)實例
這篇文章主要介紹了iOS應(yīng)用開發(fā)中對設(shè)計模式中的Strategy策略模式的運用,例子采用傳統(tǒng)的Objective-C語言代碼演示,需要的朋友可以參考下2016-03-03iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖效果
這篇文章主要介紹了iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點解析
UINavigation可以附著于導(dǎo)航控制器之中使用,也可以在controller中單獨使用,這里我們將來看iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點解析.2016-06-06