Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:51:51+00:00 2026-06-12T08:51:51+00:00

I am using UIViewContentModeScaleToFill to scale image.After scaling,Image is not showing on correct position.Help

  • 0

I am using UIViewContentModeScaleToFill to scale image.After scaling,Image is not showing on correct position.Help me.

Below is my code:

  UIImage *image=[UIImage imageNamed:@"image2.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    imageView.frame=CGRectMake(0, 0, 200, 200);
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    //change width of frame
    CGRect frame = imageView.frame;
    imageView.frame = frame;
    [self.view addSubview:imageView];

After scaling:

refer to image 1

Before scaling:

refer to image 2

I want scaling image starting from origin point (0,0).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:51:52+00:00Added an answer on June 12, 2026 at 8:51 am

    Hi try the below code i hope it help you to get idea….

     UIImage *image=[UIImage imageNamed:@"image2.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
      //  imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.contentMode = UIViewContentModeScaleAspectFit; 
        imageView.frame = self.view.bounds;
        [self.view addSubview:imageView];
    

    Update after below comment:

    I want to crop the image without changing its aspect ration.the width
    is 200px.and height will change according to width. – shivam

    you want to crop means you try the below category code:

    UIImage+MyImage.h

    #import <UIKit/UIKit.h>
    
    @interface UIImage (MyImage)
    
    
    + (UIImage*)imageFromView:(UIView*)view;
    + (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize;
    + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
    + (void)beginImageContextWithSize:(CGSize)size;
    + (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds;
    
    + (UIImage *) imageFromColor:(UIColor *)color;
    
    @end
    

    UIImage+MyImage.m

    #import "UIImage+MyImage.h"
    
    @implementation UIImage (MyImage)
    
    + (void)beginImageContextWithSize:(CGSize)size
    {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            if ([[UIScreen mainScreen] scale] == 2.0) {
                UIGraphicsBeginImageContextWithOptions(size, YES, 2.0);
            } else {
                UIGraphicsBeginImageContext(size);
            }
        } else {
            UIGraphicsBeginImageContext(size);
        }
    }
    
    + (void)endImageContext
    {
        UIGraphicsEndImageContext();
    }
    
    + (UIImage*)imageFromView:(UIView*)view
    {
        [self beginImageContextWithSize:[view bounds].size];
        BOOL hidden = [view isHidden];
        [view setHidden:NO];
        [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        [self endImageContext];
        [view setHidden:hidden];
        return image;
    }
    
    + (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
    {
        UIImage *image = [self imageFromView:view];
        if ([view bounds].size.width != newSize.width ||
            [view bounds].size.height != newSize.height) {
            image = [self imageWithImage:image scaledToSize:newSize];
        }
        return image;
    }
    
    + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
    {
        [self beginImageContextWithSize:newSize];
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        [self endImageContext];
        return newImage;
    }
    
    
    + (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds {
        CGImageRef imageRef = CGImageCreateWithImageInRect(myImage.CGImage, bounds);
        UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        CGSize asd =  croppedImage.size;
        return croppedImage;
    }
    
    
    
    
    
    + (UIImage *) imageFromColor:(UIColor *)color {
        CGRect rect = CGRectMake(0, 0, 1, 1);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [color CGColor]);
        //  [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;
        CGContextFillRect(context, rect);
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    
    
    
    
    @end
    

    the call will be :

    imageView.image=[UIImage croppedImage:imageView.Image :CGRectMake(0,0,200,200) ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Behold: //UIImageView* stretchTest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@image.png]]; //[self addSubview:stretchTest]; UIButton *stretchTest = [UIButton
Using CSS, how can I create a partial border as per image below I
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using Yii, I want to delete all the rows that are not from today.
Using the opencart image manager how would I set the upload to automatically remove:
using System; f.involk() failed since it needs a string parameter, how to correct the
Using the ndk I have compiled a code written in C. The program is
I am using a custom image as the background image for my buttons. I
I'm doing this to load an image into a tableview cell: UIImage *image =
Using TortoiseSVN against VisualSVN I delete a source file that I should not have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.