(IOS)上传图片太大,压缩图片

  -(UIImage *)resizeImage:(UIImage *)image

    {

        floatactualHeight = image.size.height;

        floatactualWidth = image.size.width;

        floatmaxHeight =300.0;

        floatmaxWidth =400.0;

        floatimgRatio = actualWidth/actualHeight;

        floatmaxRatio = maxWidth/maxHeight;

        floatcompressionQuality =0.5;//50 percent compression


        if(actualHeight > maxHeight || actualWidth > maxWidth)

        {

            if(imgRatio < maxRatio)

            {

                //adjust width according to maxHeight

                imgRatio = maxHeight / actualHeight;

                actualWidth = imgRatio * actualWidth;

                actualHeight = maxHeight;

            }

            elseif(imgRatio > maxRatio)

            {

                //adjust height according to maxWidth

                imgRatio = maxWidth / actualWidth;

                actualHeight = imgRatio * actualHeight;

                actualWidth = maxWidth;

            }

            else

            {

                actualHeight = maxHeight;

                actualWidth = maxWidth;

            }

        }


        CGRect rect = CGRectMake(0.0,0.0, actualWidth, actualHeight);

        UIGraphicsBeginImageContext(rect.size);

        [image drawInRect:rect];

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);

        UIGraphicsEndImageContext();


        return[UIImage imageWithData:imageData];


    }

你可能感兴趣的:((IOS)上传图片太大,压缩图片)