ios壓縮圖片大小

- (UIImage *)scaleFromImage:(UIImage *)imageio

{im

    if (!image)data

    {new

        return nil;return

    }

    NSData *data =UIImagePNGRepresentation(image);

    CGFloat dataSize = data.length/1024;

    CGFloat width  = image.size.width;

    CGFloat height = image.size.height;

    CGSize size;

    

    if (dataSize<=50)//小於50k

    {

        return image;

    }

    else if (dataSize<=100)//小於100k

    {

        size = CGSizeMake(width/1.f, height/1.f);

    }

    else if (dataSize<=200)//小於200k

    {

        size = CGSizeMake(width/2.f, height/2.f);

    }

    else if (dataSize<=500)//小於500k

    {

        size = CGSizeMake(width/2.f, height/2.f);

    }

    else if (dataSize<=1000)//小於1M

    {

        size = CGSizeMake(width/2.f, height/2.f);

    }

    else if (dataSize<=2000)//小於2M

    {

        size = CGSizeMake(width/2.f, height/2.f);

    }

    else//大於2M

    {

        size = CGSizeMake(width/2.f, height/2.f);

    }

    

    NSLog(@"%f,%f",size.width,size.height);

    

    UIGraphicsBeginImageContext(size);

    

    [image drawInRect:CGRectMake(0,0, size.width, size.height)];

    

    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    if (!newImage)

    {

        return image;

    }

    

    return newImage;

}

相關文章
相關標籤/搜索