欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)容了,希望大家能夠喜歡。

相關(guān)文章

  • iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄

    iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 詳解iOS平臺(tái)調(diào)用后臺(tái)接口的正確姿勢(shì)

    詳解iOS平臺(tái)調(diào)用后臺(tái)接口的正確姿勢(shì)

    這篇文章主要介紹了詳解iOS平臺(tái)調(diào)用后臺(tái)接口的正確姿勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 簡(jiǎn)單說說iOS之WKWebView的用法小結(jié)

    簡(jiǎn)單說說iOS之WKWebView的用法小結(jié)

    iOS8.0之后我們使用 WebKit框架中的WKWebView來加載網(wǎng)頁(yè)。這篇文章主要介紹了簡(jiǎn)單說說iOS之WKWebView的用法小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • iOS用AutoLayout實(shí)現(xiàn)分頁(yè)滾動(dòng)功能

    iOS用AutoLayout實(shí)現(xiàn)分頁(yè)滾動(dòng)功能

    這篇文章主要給大家介紹了關(guān)于iOS用AutoLayout實(shí)現(xiàn)分頁(yè)滾動(dòng)功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • iOS如何裁剪圓形頭像

    iOS如何裁剪圓形頭像

    這篇文章主要介紹了iOS如何裁剪圓形頭像的方法,如何為圓形頭像加邊框,如何進(jìn)行截圖操作,感興趣的小伙伴們可以參考一下
    2016-04-04
  • iOS應(yīng)用運(yùn)用設(shè)計(jì)模式中的Strategy策略模式的開發(fā)實(shí)例

    iOS應(yīng)用運(yùn)用設(shè)計(jì)模式中的Strategy策略模式的開發(fā)實(shí)例

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中對(duì)設(shè)計(jì)模式中的Strategy策略模式的運(yùn)用,例子采用傳統(tǒng)的Objective-C語言代碼演示,需要的朋友可以參考下
    2016-03-03
  • iOS使用UIScrollView實(shí)現(xiàn)無限循環(huán)輪播圖效果

    iOS使用UIScrollView實(shí)現(xiàn)無限循環(huán)輪播圖效果

    這篇文章主要介紹了iOS使用UIScrollView實(shí)現(xiàn)無限循環(huán)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • iOS實(shí)現(xiàn)音頻進(jìn)度條效果

    iOS實(shí)現(xiàn)音頻進(jìn)度條效果

    這篇文章主要介紹了ios實(shí)現(xiàn)音頻進(jìn)度條效果,本文寫了一個(gè)小demo通過實(shí)例代碼相結(jié)合的形式給大家詳細(xì)介紹,需要的朋友可以參考下
    2018-10-10
  • iOS繪制3D餅圖的實(shí)現(xiàn)方法

    iOS繪制3D餅圖的實(shí)現(xiàn)方法

    餅圖常用于統(tǒng)計(jì)學(xué)模塊。常見的一般為2D餅圖,這篇文章主要介紹了iOS繪制3D餅圖的實(shí)現(xiàn)方法,3D餅圖更加立體,用戶的好感度也比較高,下面需要的朋友可以參考借鑒,一起來看看吧。
    2017-01-01
  • iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析

    iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析

    UINavigation可以附著于導(dǎo)航控制器之中使用,也可以在controller中單獨(dú)使用,這里我們將來看iOS中的導(dǎo)航欄UINavigationBar與工具欄UIToolBar要點(diǎn)解析.
    2016-06-06

最新評(píng)論