ios中圖像進(jìn)行壓縮方法匯總
更新時(shí)間:2015年05月27日 11:24:49 投稿:hebedich
在Iphone上有兩種讀取圖片數(shù)據(jù)的簡(jiǎn)單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數(shù)需要兩個(gè)參數(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)建一個(gè)bitmap的context
// 并把它設(shè)置成為當(dāng)前正在使用的context
UIGraphicsBeginImageContext(size);
// 繪制改變大小的圖片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 從當(dāng)前context中創(chuàng)建一個(gè)改變大小后的圖片
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
方法三:(本人項(xiàng)目中使用的方法)
復(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;
}
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼
- IOS 時(shí)間和時(shí)間戳之間轉(zhuǎn)化示例
- iOS獲取當(dāng)前時(shí)間和當(dāng)前時(shí)間戳的方法
- iOS開(kāi)發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
- iOS長(zhǎng)按UIlabel實(shí)現(xiàn)可復(fù)制功能
- iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
- iOS應(yīng)用開(kāi)發(fā)中UIView添加邊框顏色及設(shè)置圓角邊框的方法
- iOS實(shí)現(xiàn)壓縮圖片上傳功能
- iOS常用小功能(獲得屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size)
相關(guān)文章
解決Flutter出現(xiàn)CocoaPods報(bào)錯(cuò)情況(Mac和IOS)
這篇文章主要為大家介紹了解決Flutter出現(xiàn)CocoaPods報(bào)錯(cuò)情況(Mac和IOS)的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Xcode8以及iOS10適配等常見(jiàn)問(wèn)題匯總(整理篇)
隨著iOS 10的更新以及Xcdoe 8的更新出現(xiàn)了很多問(wèn)題,今天小編抽時(shí)間給大家整理下我遇到的坑特此分享到腳本之家平臺(tái),供大家參考2016-09-09
iOS開(kāi)發(fā)實(shí)現(xiàn)抽屜效果
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)抽屜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解
這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

