Skip to content

Instantly share code, notes, and snippets.

@paingpyi
Created June 26, 2013 06:12
Show Gist options
  • Save paingpyi/5865100 to your computer and use it in GitHub Desktop.
Save paingpyi/5865100 to your computer and use it in GitHub Desktop.
Display Image In Fixed-Height ScrollView. If Image width is out of bound, user can scroll horizontally. If image width is less than bound, will sit in center. Display Image In Fixed-Height UIView. If image width is less than bound, will sit in center.
#pragma mark - UIImage
/*
* ScrollView must use only TopLeft AutoResizing Option
*/
+ (void)displayImageInFixedHeightScrollView:(UIScrollView*)imgScrollView imageView:(UIImageView*)imageView image:(UIImage*)image
{
CGSize finalSize = image.size;
// Adjust ImageVieww Size
if (imageView.bounds.size.width > image.size.width && imageView.bounds.size.height >= image.size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}else if(imageView.bounds.size.height < image.size.height){
CGRect r = imageView.frame;
r.size.width = imageView.frame.size.height*(image.size.width/image.size.height);//130*(512/340)
imageView.frame = r;
imageView.contentMode = UIViewContentModeScaleAspectFit;
finalSize = r.size;
}
// Adjust ScrollView Size
CGSize rr = imgScrollView.contentSize;
rr.width = finalSize.width <= imgScrollView.frame.size.width ? imgScrollView.frame.size.width : finalSize.width;
imgScrollView.contentSize = rr;
// put the ImageView in center if image.width is smaller than screenwidth
float c = (imgScrollView.contentSize.width/2) - (finalSize.width/2); // set center point
CGRect p = imageView.frame;
p.origin.x = finalSize.width <= imgScrollView.frame.size.width-50 ? c : p.origin.x; // above 270 width images are taking the full space already.
imageView.frame = p;
NSLog(@"imageFrame : W:%f / h:%f",image.size.width,image.size.height);
NSLog(@"imageViewView : W:%f / h:%f",imageView.frame.size.width,imageView.frame.size.height);
}
+ (void)displayImageInFixedHeightView:(UIView*)view imageView:(UIImageView*)imageView image:(UIImage*)image
{
CGSize finalSize = image.size;
// Adjust ImageVieww Size
if (imageView.bounds.size.width > image.size.width && imageView.bounds.size.height >= image.size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}else if(imageView.bounds.size.height < image.size.height){
CGRect r = imageView.frame;
r.size.width = imageView.frame.size.height*(image.size.width/image.size.height);//130*(512/340)
imageView.frame = r;
imageView.contentMode = UIViewContentModeScaleAspectFit;
finalSize = r.size;
}
// put the ImageView in center if image.width is smaller than screenwidth
float c = (view.frame.size.width/2) - (finalSize.width/2); // set center point
CGRect p = imageView.frame;
p.origin.x = finalSize.width <= view.frame.size.width-50 ? c : p.origin.x; // above 270 width images are taking the full space already.
imageView.frame = p;
NSLog(@"imageFrame : W:%f / h:%f",image.size.width,image.size.height);
NSLog(@"imageViewView : W:%f / h:%f",imageView.frame.size.width,imageView.frame.size.height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment