iOS開發之UIImage等比縮放

jopen 9年前發布 | 34K 次閱讀 UIImage iOS開發 移動開發

前面講了截取UIImage指定大小區域,很方便的截取UIImage。今天要和大家分享的是UIImage的縮放。

兩種縮放:

  1. 縮放到指定大小,也就是指定的size.
  2. 等比縮放。
  3. </ol>

    縮放到指定大小

    - (UIImage*)imageCompressWithSimple:(UIImage*)image scaledToSize:(CGSize)size
    {
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0,0,size.width,size.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }

    等比縮放

    通過縮放系數

    - (UIImage*)imageCompressWithSimple:(UIImage*)image scale:(float)scale
    {
        CGSize size = image.size;
        CGFloat width = size.width;
        CGFloat height = size.height;
        CGFloat scaledWidth = width * scale;
        CGFloat scaledHeight = height * scale;
        UIGraphicsBeginImageContext(size); // this will crop
        [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
        UIImage* newImage= UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }

    scale是縮放系數 。

    通過計算得到縮放系數

    - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
    {

    UIImage *sourceImage = [UIImage imageNamed:@"test.jpg"];
    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;
    
    

    }</pre>

    很久之前寫了一篇文章解決MWPhotoBrowser中的SDWebImage加載大圖導致的內存警告問題。這個我記得當時從服務器拿到的照片大概有10幾M的樣子,加載出來會導致內存警告。所以我當時通過修改SDWebImage源碼,就是把下載下來的照片進行縮放,內存就降下來了。一般情況下應該不會加載這么大的照片的,用戶要是知道,早把你的APP給刪掉了。

    Posted by 李剛 Jul 29th, 2015 12:01 am ios開發
    來源:剛剛在線(微信:iOSDevTip),歡迎分享本文,轉載請保留出處!
    原文鏈接:http://www.superqq.com/blog/2015/07/29/uiimage-geometric-zoom/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!