ios 圖片處理( 1.按比例縮放 2.指定寬度按比例縮放

來源:互聯網
上載者:User

標籤:

本文轉載至 http://blog.sina.com.cn/s/blog_6f29e81f0101tat6.html //按比例縮放,size 是你要把圖顯示到 多大地區 CGSizeMake(300, 140)

-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{

 

    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    CGFloat targetWidth = size.width;

    CGFloat targetHeight = size.height;

    CGFloat scaleFactor = 0.0;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

 

    if(CGSizeEqualToSize(imageSize, size) == NO){

 

        CGFloat widthFactor = targetWidth / width;

        CGFloat heightFactor = targetHeight / height;

 

        if(widthFactor > heightFactor){

            scaleFactor = widthFactor;

 

        }

        else{

 

            scaleFactor = heightFactor;

        }

        scaledWidth = width * scaleFactor;

        scaledHeight = height * scaleFactor;

 

        if(widthFactor > heightFactor){

 

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

        }else if(widthFactor < heightFactor){

 

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

        }

    }

 

    UIGraphicsBeginImageContext(size);

 

    CGRect thumbnailRect = CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

 

    [sourceImage drawInRect:thumbnailRect];

 

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil){

        NSLog(@"scale image fail");

    }

 

    UIGraphicsEndImageContext();

    return newImage;

}

 

 

//指定寬度按比例縮放

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{

 

    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    CGFloat targetWidth = defineWidth;

    CGFloat targetHeight = height / (width / targetWidth);

    CGSize size = CGSizeMake(targetWidth, targetHeight);

    CGFloat scaleFactor = 0.0;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

 

    if(CGSizeEqualToSize(imageSize, size) == NO){

 

        CGFloat widthFactor = targetWidth / width;

        CGFloat heightFactor = targetHeight / height;

 

        if(widthFactor > heightFactor){

            scaleFactor = widthFactor;

        }

        else{

            scaleFactor = heightFactor;

        }

        scaledWidth = width * scaleFactor;

        scaledHeight = height * scaleFactor;

 

        if(widthFactor > heightFactor){

 

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

 

        }else if(widthFactor < heightFactor){

 

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

        }

    }

 

    UIGraphicsBeginImageContext(size);

 

    CGRect thumbnailRect = CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

 

    [sourceImage drawInRect:thumbnailRect];

 

    newImage = UIGraphicsGetImageFromCurrentImageContext();

 

    if(newImage == nil){

 

        NSLog(@"scale image fail");

    }

    UIGraphicsEndImageContext();

    return newImage;

}

ios 圖片處理( 1.按比例縮放 2.指定寬度按比例縮放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.