Home one image on to another image size issue ios
Reply: 1

one image on to another image size issue ios

Mayank Patel
1#
Mayank Patel Published in 2018-01-13 07:03:08Z

I want to add one small image to another main image something like badge I am doing this code for this but problem is if background image is small then watermark is showing very large. Please provide any advice.

[_productImg sd_setImageWithURL:[NSURL URLWithString:imgUrl]
                   placeholderImage:nil
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                              if (image.size.width > 0 && img.isdefaultImage) {
                                  UIImage *watermark = [UIImage imageNamed:@"is_default_big"];
                                  CGFloat scale = image.scale;
                                  UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
                                   [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
                                  [watermark drawInRect:CGRectMake(image.size.width - watermark.size.width, 0, watermark.size.width, watermark.size.height)];
                                  UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
                                  UIGraphicsEndImageContext();
                                  _productImg.image = result;

                              }

                              [_spinner stopAnimating];
                              [_spinner removeFromSuperview];

                          }];


      });

LGP
2#
LGP Reply to 2018-01-13 08:51:03Z

Since your images apparently have varying sizes, but you seem to show them scaled to (roughly) the same size, you need to scale your badge.

Try something like this:

// Add this line
CGFloat desiredBadgeSize = 0.2; // Badge is 20% of image height

// Change these lines
CGFloat scale = image.size.height / watermark.size.height * desiredBadgeSize;
[watermark drawInRect:CGRectMake(image.size.width - watermark.size.width * scale, 0, watermark.size.width * scale, watermark.size.height * scale)];

The code above assumes that you scale your images to the same height on your device.

Another approach, which usually is faster, is put the badge in an UIImageView and add that to the view containing you image. That way you still have your original image, and can easily remove the badge too if you need to.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.354888 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO