11

UIImagePNGRepresentation画像保存に使っています。結果の画像のサイズは 30+ KB で、私の場合は BIG です。

使用UIImageJPEGRepresentationしてみましたが、画像を圧縮できるため、画像は5KB未満のサイズで保存されますが、JPEGで保存すると背景が白くなり、望ましくありません(私の画像は円形なので、保存する必要があります透明な背景)。

を使用して画像サイズを圧縮するにはどうすればよいUIImagePNGRepresentationですか?

4

2 に答える 2

0

これはあなたを助けるかもしれません:

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

    NSData *finalData = nil;
    NSData *unscaledData = UIImagePNGRepresentation(image);

    if (unscaledData.length > 5000.0f ) {


       //if image size is greater than 5KB dividing its height and width maintaining proportions


        UIImage *scaledImage = [self imageWithImage:image andWidth:image.size.width/2 andHeight:image.size.height/2];
        finalData = UIImagePNGRepresentation(scaledImage);

        if (finalData.length > 5000.0f ) {

            [self resizeImage:scaledImage];
        }
        //scaled image will be your final image
    }
}

画像のサイズ変更

- (UIImage*)imageWithImage:(UIImage*)image andWidth:(CGFloat)width andHeight:(CGFloat)height
{
    UIGraphicsBeginImageContext( CGSizeMake(width, height));
    [image drawInRect:CGRectMake(0,0,width,height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext() ;
    return newImage;
}
于 2016-02-11T19:54:59.203 に答える