13

I'm using zoul's solution to export UIImage array as a movie. But my frames all turns out distorted. Here's the original image:

original image

Here's a similar distortion example:
similar distortion example

I've read here that it has something to do with aspect ratio, but there's no explanation on how to fix it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Geva
  • 820
  • 6
  • 14

3 Answers3

46

After a lot of experiments with different image sizes, and with the help of this article, I've got to the conclusion that the width of the images must be a multiple of 16.

Geva
  • 820
  • 6
  • 14
  • 3
    Wasted many hours wondering what was going on ... this was the key! – DogCoffee Jan 25 '14 at 07:19
  • 1
    This finally did it! Thanks so much. – Linuxios May 19 '15 at 17:48
  • thank you so much! i was struggling with this for hours! this fixed it! – sudoExclaimationExclaimation Aug 11 '15 at 06:01
  • Do we know why this is the case? –  Aug 28 '15 at 11:12
  • Great great great !!! guys the best size is 1024 x 1024 , just convert you image to 1024 – iOS.Lover Jan 01 '16 at 17:32
  • BTW - width:height vs height:width matters. I thought 1080:1920 would work but its the width that needs to be a multiple of 16 - had to downscale to 1024:1820 – Cbas Jun 30 '16 at 18:27
  • @Cbas how did you down scale? I have some images in an array. Shall I draw them in bitamp context forcing the width to be a multiple of 16 and at the same time maintaining the aspect ratio of height parameter? – nr5 Jul 01 '17 at 19:40
  • Okay, but what do I do if I really need that 1080x1920 image resolution? It's absolutely a requirement for my purposes, because I have a FullHD video, I take the last frame out of it, generate a 3 second video from this frame, and then combine those two videos, and since I want to preserve the best quality, I need that 1080x1920 image. – xinatanil May 17 '19 at 10:07
  • @xinatanil Is it a portrait? Maybe try to rotate it, export, and then rotate again? Or using a 1072x1,905.7778 size? – Geva May 17 '19 at 21:03
  • @Geva I went with 1008x1792 image size, which is a viable solution for me, but my inner perfectionist is still mad with the fact that I cannot generate a 1080x1920 image properly from the beginning. – xinatanil May 22 '19 at 10:37
3

For those still doing the journey in 2020, and getting distortion in their movies because its not width 16px

change

CGContextRef context = CGBitmapContextCreate(pxdata,
                                             width, height,
                                             8, 4 * width,
                                             rgbColorSpace,
                                             kCGImageAlphaNoneSkipFirst);

to

CGContextRef context = CGBitmapContextCreate(pxdata,
                                             width, height,
                                             8, CVPixelBufferGetBytesPerRow(pxbuffer),
                                             rgbColorSpace,
                                             kCGImageAlphaNoneSkipFirst);

Credit to @Bluedays Output from AVAssetWriter (UIImages written to video) distorted

Daryl Teo
  • 5,394
  • 1
  • 31
  • 37
2

I'v encountered this problem too, and I found the reason is the image's size is not the multiple of 16, you can change the size and have a try.

Eric.Cheng
  • 162
  • 2
  • 7
  • Please clarify what you mean. Do you mean the image size does not *actually* have to be 16 - and that you merely have to specify the width? If so, where do you specify the width? – Praxiteles Jun 11 '15 at 23:16