Pregunta

I'm trying to resize the UIImage

Before the UIImage: enter image description here

After this code and UIImage:

UIGraphicsBeginImageContext (CGPointMake(155,139));
[currentImageView.image drawInRect:CGRectMake(0,0,155,139)];
currentImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"get size : %@",NSStringFromCGSize(currentImageView.image.size));

NSLog: get size : {155,139}

Last, I try to restore the size by this code. but its blurry.

UIGraphicsBeginImageContext (OriginalSize);
[currentImageView.image drawInRect:CGRectMake(0,0,OriginalSize.width,OriginalSize.height)];
currentImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"get size : %@",NSStringFromCGSize(cnurrentImageView.image.size));

NSLog: get size : {1024,352}

What happened? Have any suggestions and advice?

¿Fue útil?

Solución

You are making two mistakes,

1.Reducing the image size and setting it to a larger size imageView, this can be fixed with the code,

    imageView.contentMode = UIViewContentModeScaleAspectFit;

2.You have resize the original image to a smaller size, then resizing the smaller image to original size, this will stretch the image.

Otros consejos

FOR 1 : first step,

You can use this code so that the image quality doesn't loose:

Instead of

UIGraphicsBeginImageContext

Use

UIGraphicsBeginImageContextWithOptions(currentImageView.frame.size, NO, 0);

as the size you want is smaller and the imageview is larger so you also need to set

YourimageView.contentMode = UIViewContentModeScaleAspectFit;

For Second Step :

Use original image to resize it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top