国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS中將圖像進行壓縮的三種方法

2020-02-19 15:57:42
字體:
來源:轉載
供稿:網友

方法一:

?


- (UIImage*)scaleFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
?CGSize imageSize = image.size;
?CGFloat width = imageSize.width;
?CGFloat height = imageSize.height;
?????
?if (width ? return image;
?}
?????
?if (width == 0 || height == 0){
? return image;
?}
?????
?CGFloat widthFactor = newSize.width / width;
?CGFloat heightFactor = newSize.height / height;
?CGFloat scaleFactor = (widthFactor ?????
?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


#import ?
@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 ??????????? {?
??????????????? 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;
}

?

?

?

以上所述就是IOS中將圖像進行壓縮的三種方法,希望大家能夠喜歡,更多內容請繼續關注武林技術頻道網站!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 逊克县| 平邑县| 宜兰县| 法库县| 城口县| 石门县| 湖南省| 仲巴县| 砀山县| 望都县| 杭锦旗| 四子王旗| 双峰县| 鄯善县| 茌平县| 青神县| 聊城市| 巍山| 定日县| 北流市| 郴州市| 布尔津县| 留坝县| 襄樊市| 历史| 静乐县| 出国| 临夏县| 镶黄旗| 河间市| 前郭尔| 宜良县| 合阳县| 大宁县| 高邑县| 锦屏县| 杭锦后旗| 黎川县| 永平县| 南和县| 抚州市|