Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this code that I used for resizing image and you will get the new frame as well.The ratio seem to be fixed but you can change it as per your requirement.</p> <pre><code>-(UIImage*)ImageResize:(UIImage*)image { if(image==NULL) { return NULL; } else { float actualHeight = image.size.height; float actualWidth = image.size.width; float imgRatio = actualWidth/actualHeight; float maxRatio = 130.0/160.0; if(imgRatio!=maxRatio) { if(imgRatio &lt; maxRatio) { imgRatio = 160.0 / actualHeight; actualWidth = imgRatio * actualWidth; actualHeight = 160.0; } else { imgRatio = 130.0 / actualWidth; actualHeight = imgRatio * actualHeight; actualWidth = 130.0; } } CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); UIGraphicsBeginImageContext(rect.size); [image drawInRect:rect]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } } </code></pre> <p>The below code can be used for specific image size that you can pass.</p> <pre><code> -(UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)wantSize { UIImage * targetImage; if (nil == image) { targetImage = nil; }else{ CGSize size = image.size; CGRect rect; if (wantSize.width/wantSize.height &gt; size.width/size.height) { rect.size.width = wantSize.height*size.width/size.height; rect.size.height = wantSize.height; rect.origin.x = (wantSize.width - rect.size.width)/2; rect.origin.y = 0; } else{ rect.size.width = wantSize.width; rect.size.height = wantSize.width*size.height/size.width; rect.origin.x = 0; rect.origin.y = (wantSize.height - rect.size.height)/2; } UIGraphicsBeginImageContext(wantSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); UIRectFill(CGRectMake(0, 0, wantSize.width, wantSize.height));//clear background [image drawInRect:rect]; targetImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return targetImage; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload