Reputation: 67
I'm trying to resize the UIImage
Before the UIImage:
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?
Upvotes: 2
Views: 1089
Reputation: 286
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.
Upvotes: 0
Reputation: 15115
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.
Upvotes: 3