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

首頁 > 網站 > 建站經驗 > 正文

ios中圖像-進行壓縮方法匯總

2019-11-02 14:32:10
字體:
來源:轉載
供稿:網友

   在Iphone上有兩種讀取圖片數據的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數需要兩個參數:圖片的引用和壓縮系數.而UIImagePNGRepresentation只需要圖片引用作為參數.

  方法一:

  代碼如下:

  - (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

  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;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 颍上县| 新营市| 宁晋县| 富阳市| 田阳县| 上高县| 宣城市| 永寿县| 宣城市| 松阳县| 龙山县| 三台县| 洛扎县| 博湖县| 河北区| 平罗县| 称多县| 江口县| 重庆市| 新野县| 青田县| 澎湖县| 安图县| 昌乐县| 平谷区| 泸溪县| 石屏县| 三明市| 峡江县| 青海省| 安多县| 英山县| 宝兴县| 高雄县| 宜君县| 凌云县| 曲阳县| 岳阳市| 台东县| 文水县| 仙桃市|