ios 图片转换大小

@implementation ImageSizeManager

#define iphoneSize [[UIScreen mainScreen] bounds]

#define newSizeWidth [[UIScreen mainScreen] bounds].size.width * 2.0
#define newSizeHeight [[UIScreen mainScreen] bounds].size.height * 2.0

//转换成大图片
+(UIImage*)getMaxImageWithOldImage:(UIImage*)oldImage{
    CGSize newSize = [self changeImage:oldImage.size andScale:2];
    UIImage *newImage = [self imageWithImageSimple:oldImage scaledToSize:newSize];
    return newImage;
}
//转换中图片
+(UIImage*)getMidImageWithOldImage:(UIImage*)oldImage{
    CGSize newSize = [self changeImage:oldImage.size andScale:4];
    UIImage *newImage = [self imageWithImageSimple:oldImage scaledToSize:newSize];
    return newImage;
}
//转换成小图片
+(UIImage*)getSmallImageWithOldImage:(UIImage*)oldImage{
    CGSize newSize = [self changeImage:oldImage.size andScale:8];
    UIImage *newImage = [self imageWithImageSimple:oldImage scaledToSize:newSize];
    return newImage;
}

+(CGSize)changeImage:(CGSize)size andScale:(float)scale
{
    float newWidth =  newSizeWidth/scale;
    float newHeight = newSizeHeight/scale;
    CGSize newSize;
    
    if (size.width <=newWidth && size.height <=newHeight) {
        return size;
    }
    if (size.width>newWidth || size.height >newHeight) {
        if (size.width > size.height) {
            //如果宽大,就把宽度缩小到,高按同比例缩放
            newSize.width = newWidth;
            newSize.height = (int) ((newWidth/size.width) * size.height);
        }
        else if(size.width < size.height)
        {
            newSize.height = newHeight;
            newSize.width =(int) ((newHeight/size.height) * size.width);
        }else//宽高相同
        {
            newSize.width = newWidth;
            newSize.height = newWidth;
        }
    }
    return newSize;
}
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data = UIImageJPEGRepresentation(newImage, 1.0);
    DLog(@"长度:%d ",data.length);
    DLog(@"长:%f 宽:%f",newImage.size.height,newImage.size.width);
    return newImage;
}
 

  

转载于:https://www.cnblogs.com/zhangsongbai/p/3580301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值