zoukankan      html  css  js  c++  java
  • <12>保持原来的长宽比,生成一个缩略图

    /*
     * 保持原来的长宽比,生成一个缩略图
     * &-image 待传入UIImage
     * &-size 待传入UIImage要改变图像的尺寸
     * 返回处理好的UIImage
     */
    + (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)siz/** * 保持原来的长宽比,生成一个缩略图 */
    + (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)size
    {
        UIImage *newimage;
        if (nil == image) {
            newimage = nil;
        }
    else{
            CGSize oldsize = image.size;
            CGRect rect;
            if (size.width/size.height > oldsize.width/oldsize.height) {
                rect.size.width = size.height*oldsize.width/oldsize.height;
                rect.size.height = size.height;
                rect.origin.x = (size.width - rect.size.width)/2;
                rect.origin.y = 0;
            }
            else{
                rect.size.width = size.width;
                rect.size.height = size.width*oldsize.height/oldsize.width;
                rect.origin.x = 0;
                rect.origin.y = (size.height - rect.size.height)/2;
            }
            UIGraphicsBeginImageContext(size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
            UIRectFill(CGRectMake(0, 0, size.width, size.height));//clear background
            [image drawInRect:rect];
            newimage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        return newimage;
    }
     
  • 相关阅读:
    数据包发送
    linux 进程调度3
    linux 进程调度2
    linux 进程调度1
    进程间通信:信号
    fork vfork clone学习
    跳表
    【转】Linux内存管理综述
    如何优雅的写出链表代码
    This function or variable may be unsafe Consider using xxx instead
  • 原文地址:https://www.cnblogs.com/iQingYang/p/6687948.html
Copyright © 2011-2022 走看看