zoukankan      html  css  js  c++  java
  • 修改图片尺寸

    @interface UIImage (Category)
    - (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height;
    @end
    @implementation UIImage (Category)
    - (UIImage*)transformWidth:(CGFloat)width 
    height:(CGFloat)height {
    
    CGFloat destW = width;
    CGFloat destH = height;
    CGFloat sourceW = width;
    CGFloat sourceH = height;
        
    CGImageRef imageRef = self.CGImage;
    CGContextRef bitmap = CGBitmapContextCreate(NULL, 
    destW, 
    destH,
    CGImageGetBitsPerComponent(imageRef), 
    4*destW, 
    CGImageGetColorSpace(imageRef),
    (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
    
    CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);
    
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImageimageWithCGImage:ref];
    CGContextRelease(bitmap);
    CGImageRelease(ref);
    return result;
    }
    @end
    +(UIImage*)compressImageDownToPhoneScreenSize:(UIImage*)theImage{
    UIImage * bigImage = theImage;
    float actualHeight = bigImage.size.height;
    float actualWidth = bigImage.size.width;
    
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = 480.0 / 640;
    
    if(imgRatio != maxRatio ){
    if(imgRatio < maxRatio){
    imgRatio = 480.0 / actualHeight;
    actualWidth = imgRatio * actualWidth;
    actualHeight = 480.0;
    
    } else {
    imgRatio = 320.0 / actualWidth;
    actualHeight = imgRatio * actualHeight;     
    actualWidth = 320.0;
    }
    }
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [bigImage drawInRect:rect];  // scales image to rect
    theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
    }
  • 相关阅读:
    input框限制只能输入正整数、字母、小数、
    css水平垂直居中
    Android开发之旅-获取地理位置的经度和纬度
    Android DDMS应用
    Android开发BUG及解决方法2
    Android开发BUG及解决方法1
    Android开发BUG及解决方法
    Android系统架构
    1.sql简介
    C语言笔试常考知识点
  • 原文地址:https://www.cnblogs.com/appwgh/p/2517485.html
Copyright © 2011-2022 走看看