2

I want to draw UIImage for pattern background and I need need it be float size. For example: I need 7.0699929299999997 width and height.

enter image description here

My code:

UIImage *repeatedImage = [UIImage imageNamed:@"LayoutRepeatedImage"];
    CGSize destinationSize = CGSizeMake(7.0699929299999997, 7.0699929299999997);
    UIGraphicsBeginImageContext(destinationSize);
    [repeatedImage drawInRect:CGRectMake(0, 0, destinationSize.width, destinationSize.height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

But as result my image:

po resizedImage
<UIImage: 0x6000014b21e0> size {8, 8} orientation 0 scale 1.000000

Help me out please

Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
Artem Z.
  • 1,243
  • 2
  • 14
  • 36
  • 2
    `UIImage`s resolution is in pixels. You can't have a size that isn't an Integer, even if you provide a `CGFloat` in the `CGSizeMake` it'll be converted to an Integer later. – TawaNicolas Oct 27 '17 at 09:35
  • @TawaNicolas thanks for your reply, it's really bad ;( – Artem Z. Oct 27 '17 at 09:37
  • Why is it bad? Why would you need to do something like that anyways? – TawaNicolas Oct 27 '17 at 09:41
  • @TawaNicolas I need a very large background with dots like this. I draw elements above this dots like grid. https://puu.sh/y8leO/63c688ef47.png And each px is important – Artem Z. Oct 27 '17 at 09:46
  • And each px is important -> But it is not possible to get floating value in CGSize and also no floating point in pixel available , You can roundup your value and draw – Prashant Tukadiya Oct 27 '17 at 10:09
  • 1
    @ArtemZ. Your draw image is create properly like you want but you print "po resizedImage" then you can get size an integer like size {8, 8} because it's not getting floating value. so don't worry about. – BuLB JoBs Oct 27 '17 at 10:19
  • if you want check actual image size then download image in document directory path and open in image preview. – BuLB JoBs Oct 27 '17 at 10:24

1 Answers1

0

You can draw fractional sizes into image but remember:

  1. pixel is the smallest unit, image is made from pixels, you can not have half pixel, just full pixels.
  2. If you draw fractional size into image, that fraction will cause that from final color drawn you get only percentage based on how much it covers the pixel. So for your value 7.06999, 7 pixels will get full color, last one only 6% of color in the image drawn. In the end it may look blurry on that side.
Juraj Antas
  • 3,059
  • 27
  • 37