1

I have added an Imageview on UIView, and set the property AspectToFit. According to app requirement, I need to add one more image on the imageview.

iPhone 6s Plus

    float width;
    float height;
    float x;
    float y;

    CGSize imageViewSize =  self.getImageView.bounds.size;// self.getImageView.frame.size;
    CGSize imageSize = self.getImage.size;
    CGFloat minFactor = imageViewSize.width / imageSize.width;
    if (imageViewSize.height / imageSize.height < minFactor)
    {
        minFactor = imageViewSize.height / imageSize.height;
    }
    CGRect frame = self.getImageView.frame;
    NSLog(@"view height..%@",NSStringFromCGRect (frame));
    CGSize resultSize = CGSizeMake(minFactor * imageSize.width, minFactor * imageSize.height);
    UIImage *newimage =  [self imageWithImage:self.getImage scaledToSize:resultSize];
    float mainwidth = imageViewSize.width;
    float mainheight = imageViewSize.height;

    float imagewidth = newimage.size.width;
    float imageheight = newimage.size.height;

    _widthdiff = mainwidth-imagewidth;
    _heightdiff = mainheight-imageheight;

    x = self.view.frame.size.width-width-0-_widthdiff/2;
    y = self.view.frame.size.height-height-116-_heightdiff/2;

imageview = [[UIImageView alloc]
                 initWithFrame:CGRectMake(x, y, width, height)];
    [imageview setContentMode:UIViewContentModeScaleAspectFit];

This code is working fine for all the devices except iPhone (iPhone 6 Plus, iPone 7 Plus). As I can see there is issue only in Plus size of devices.

Is there a different to calculate the difference or ratio between imageview and proper image? Please advice.

Please refer attached imageiPhone 5s

Minkle Garg
  • 723
  • 3
  • 9
  • 35

1 Answers1

1

We can add new image by calculating the size of old image and then add it to the UIView as per requirement.

    CGSize imageViewSize =   self.getImageView.frame.size;
    CGSize imageSize = self.getImage.size;
    CGFloat minFactor = imageViewSize.width / imageSize.width;
    if (imageViewSize.height / imageSize.height < minFactor)
    {
        minFactor = imageViewSize.height / imageSize.height;
    }
    CGSize resultSize = CGSizeMake(minFactor * imageSize.width, minFactor * imageSize.height);

    UIImage *newimage =  [self imageWithImage:self.getImage scaledToSize:resultSize];
self.duplicateimageview = [[UIImageView alloc]
                                   initWithFrame:CGRectMake(self.view.frame.size.width/2-newimage.size.width/2, self.view.frame.size.height/2-newimage.size.height/2, newimage.size.width, newimage.size.height)];
        [self.view addSubview:self.duplicateimageview];
        self.duplicateimageview.image = newimage;    

- (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Minkle Garg
  • 723
  • 3
  • 9
  • 35