skip to Main Content

Hi I am developing an iOS app. I have an UIImageView with a image associated with it. I am changing its dimensions in viewDidLoad() method.

Initially when I change the dimension I am able to resize the image size on view. However after I crop the image(using Photoshop) accordingly to the shape of the object in the image(i.e getting rid of unwanted part of the image). My resize method doesn’t seem to work i.e the size of the image is not changing though I call the same method.

The method I am using for resizing is given below.

-(void)initXYZ{

CGSize size;
CGFloat x,y;

x = 0+myImageView1.frame.size.width;
y = myImageView2.center.y;
size.width = _myImageView2.frame.size.width/2;
size.height = _myImageView2.frame.size.width/2;

UIImage *image = [UIImage imageNamed:@"xyz.png"];
image = [HomeViewController imageWithImage:image scaledToSize:size xCord:x yCord:y];}

Utility method is given below

+(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize xCord:(CGFloat)X yCord:(CGFloat)Y{


UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}

2

Answers


  1. Try this

    + (UIImage*)imageWithImage:(UIImage*)image 
                   scaledToSize:(CGSize)newSize
    {
       UIGraphicsBeginImageContext( newSize );
       [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
       UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
    
       return newImage;
    }
    

    OR

    + (UIImage*)imageWithImage:(UIImage*)image 
                       scaledToSize:(CGSize)newSize {
        CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
        CGImageRef imageRef = image.CGImage;
    
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
    
        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
        CGAffineTransform flipV = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
    
        CGContextConcatCTM(context, flipV);  
    
        CGContextDrawImage(context, newRect, imageRef);
    
    
        CGImageRef newImageRef = CGBitmapContextCreateImage(context);
        UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
        CGImageRelease(newImageRef);
        UIGraphicsEndImageContext();    
    
        return newImage;
    }
    
    Login or Signup to reply.
  2. Try this:

    - (UIImage*)resizeAndStoreImages:(UIImage*)img
    {
        UIImage *chosenImage = img;
        NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
        int resizedImgMaxHeight = 500;
        int resizedImgMaxWidth = 500;
    
    
            UIImage *resizedImageData;
    
            if (chosenImage.size.height > chosenImage.size.width && chosenImage.size.height > resizedImgMaxHeight) { // portrait
    
                int width = (chosenImage.size.width / chosenImage.size.height) * resizedImgMaxHeight;
                CGRect rect = CGRectMake( 0, 0, width, resizedImgMaxHeight);
                UIGraphicsBeginImageContext(rect.size);
                [chosenImage drawInRect:rect];
                UIImage *pic1 = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
    
                resizedImageData = [UIImage imageWithData:UIImageJPEGRepresentation(pic1, 1.0)];
    
                pic1 = nil;
    
            } else if (chosenImage.size.width > chosenImage.size.height && chosenImage.size.width > resizedImgMaxWidth) { // landscape
    
                int height = (chosenImage.size.height / chosenImage.size.width) * resizedImgMaxWidth;
                CGRect rect = CGRectMake( 0, 0, resizedImgMaxWidth, height);
                UIGraphicsBeginImageContext(rect.size);
                [chosenImage drawInRect:rect];
                UIImage *pic1 = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
    
                resizedImageData = [UIImage imageWithData:UIImageJPEGRepresentation(pic1, 1.0)];
    
                pic1 = nil;
    
            } else {
    
                if (chosenImage.size.height > resizedImgMaxHeight) {
    
                    int width = (chosenImage.size.width / chosenImage.size.height) * resizedImgMaxHeight;
                    CGRect rect = CGRectMake( 0, 0, width, resizedImgMaxHeight);
                    UIGraphicsBeginImageContext(rect.size);
                    [chosenImage drawInRect:rect];
                    UIImage *pic1 = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
    
                    resizedImageData = [UIImage imageWithData:UIImageJPEGRepresentation(pic1, 1.0)];
    
                    pic1 = nil;
    
                } else {
    
                    resizedImageData = [UIImage imageWithData:imageData];
                }
            }
    
        return resizedImageData;
    
    }
    

    Adjust the resizedImgMaxHeight and resizedImgMaxWidth as per your need

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search