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;
    }
  • 相关阅读:
    argparse模块的使用
    tf.stack() /tf.unstack()
    什么是tensor
    tf.size()函数
    tf.nn.l2_loss()的用法
    CNN中的卷积
    tf.reverse()
    学习音视频编程技术 博客
    shell 批量计算MD5值
    线程池的实现
  • 原文地址:https://www.cnblogs.com/appwgh/p/2517485.html
Copyright © 2011-2022 走看看