How to convert a PNG to a WebP in Swift?












1















I'm trying to convert a PNG image in Webp in Swift, the only framework I got working is OpenCV through Objective-c++.
The problem is that if I resize the image by 512x512 (that's the resolution I need) it crashes:
enter image description here
If I resize the image (either with OpenCV either with Swift) to another resolution (ex 510x510) it doesn't crash.



The strange thing is that on the simulator it never crashes while on the iPhone XS it crashes 90% of the times.

How can I convert a PNG to a Webp in Swift?
Why is OpenCV crashing on the imwrite instruction if the Mat is 512x512?



UPDATE:

OpenCV version: 3.4.2

I found out that this problem happens when the PNG image get processed before from the Core Graphics framework. I need to use it since I save a UIVIew as UIImage this way:



let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}









share|improve this question

























  • Could you, please create a Minimal, Complete, and Verifiable example that reproduces this? (Post it as text, not an image) | What version of OpenCV?

    – Dan Mašek
    Nov 20 '18 at 22:57













  • I updated the question

    – Danny182
    Nov 20 '18 at 23:05
















1















I'm trying to convert a PNG image in Webp in Swift, the only framework I got working is OpenCV through Objective-c++.
The problem is that if I resize the image by 512x512 (that's the resolution I need) it crashes:
enter image description here
If I resize the image (either with OpenCV either with Swift) to another resolution (ex 510x510) it doesn't crash.



The strange thing is that on the simulator it never crashes while on the iPhone XS it crashes 90% of the times.

How can I convert a PNG to a Webp in Swift?
Why is OpenCV crashing on the imwrite instruction if the Mat is 512x512?



UPDATE:

OpenCV version: 3.4.2

I found out that this problem happens when the PNG image get processed before from the Core Graphics framework. I need to use it since I save a UIVIew as UIImage this way:



let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}









share|improve this question

























  • Could you, please create a Minimal, Complete, and Verifiable example that reproduces this? (Post it as text, not an image) | What version of OpenCV?

    – Dan Mašek
    Nov 20 '18 at 22:57













  • I updated the question

    – Danny182
    Nov 20 '18 at 23:05














1












1








1








I'm trying to convert a PNG image in Webp in Swift, the only framework I got working is OpenCV through Objective-c++.
The problem is that if I resize the image by 512x512 (that's the resolution I need) it crashes:
enter image description here
If I resize the image (either with OpenCV either with Swift) to another resolution (ex 510x510) it doesn't crash.



The strange thing is that on the simulator it never crashes while on the iPhone XS it crashes 90% of the times.

How can I convert a PNG to a Webp in Swift?
Why is OpenCV crashing on the imwrite instruction if the Mat is 512x512?



UPDATE:

OpenCV version: 3.4.2

I found out that this problem happens when the PNG image get processed before from the Core Graphics framework. I need to use it since I save a UIVIew as UIImage this way:



let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}









share|improve this question
















I'm trying to convert a PNG image in Webp in Swift, the only framework I got working is OpenCV through Objective-c++.
The problem is that if I resize the image by 512x512 (that's the resolution I need) it crashes:
enter image description here
If I resize the image (either with OpenCV either with Swift) to another resolution (ex 510x510) it doesn't crash.



The strange thing is that on the simulator it never crashes while on the iPhone XS it crashes 90% of the times.

How can I convert a PNG to a Webp in Swift?
Why is OpenCV crashing on the imwrite instruction if the Mat is 512x512?



UPDATE:

OpenCV version: 3.4.2

I found out that this problem happens when the PNG image get processed before from the Core Graphics framework. I need to use it since I save a UIVIew as UIImage this way:



let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}






ios swift opencv objective-c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:04







Danny182

















asked Nov 20 '18 at 22:55









Danny182Danny182

1,0371219




1,0371219













  • Could you, please create a Minimal, Complete, and Verifiable example that reproduces this? (Post it as text, not an image) | What version of OpenCV?

    – Dan Mašek
    Nov 20 '18 at 22:57













  • I updated the question

    – Danny182
    Nov 20 '18 at 23:05



















  • Could you, please create a Minimal, Complete, and Verifiable example that reproduces this? (Post it as text, not an image) | What version of OpenCV?

    – Dan Mašek
    Nov 20 '18 at 22:57













  • I updated the question

    – Danny182
    Nov 20 '18 at 23:05

















Could you, please create a Minimal, Complete, and Verifiable example that reproduces this? (Post it as text, not an image) | What version of OpenCV?

– Dan Mašek
Nov 20 '18 at 22:57







Could you, please create a Minimal, Complete, and Verifiable example that reproduces this? (Post it as text, not an image) | What version of OpenCV?

– Dan Mašek
Nov 20 '18 at 22:57















I updated the question

– Danny182
Nov 20 '18 at 23:05





I updated the question

– Danny182
Nov 20 '18 at 23:05












2 Answers
2






active

oldest

votes


















1














I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊



My wrapper is very simple but does what I needed:



#import <Foundation/Foundation.h>
#import "WebPWrapper.h"
#import "UIImage+WebP.h"

@implementation WebPWrapper
-(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
NSData *webPData = [UIImage imageToWebP:image quality:quality];
return webPData;
}
@end


In swift I use it this way:



let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)





share|improve this answer


























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Kuldeep
    Nov 21 '18 at 12:45











  • Just added the solution 😊

    – Danny182
    Nov 21 '18 at 13:07











  • @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

    – iRiziya
    Nov 29 '18 at 5:49






  • 1





    For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

    – Danny182
    Nov 29 '18 at 11:35











  • @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

    – iRiziya
    Nov 30 '18 at 12:08



















1














Swift 4.0



pod both 'SDWebImage' AND 'SDWebImage/WebP'



import SDWebImage



if let image_download = UIImage(data: data) {
let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402823%2fhow-to-convert-a-png-to-a-webp-in-swift%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊



    My wrapper is very simple but does what I needed:



    #import <Foundation/Foundation.h>
    #import "WebPWrapper.h"
    #import "UIImage+WebP.h"

    @implementation WebPWrapper
    -(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
    NSData *webPData = [UIImage imageToWebP:image quality:quality];
    return webPData;
    }
    @end


    In swift I use it this way:



    let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)





    share|improve this answer


























    • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

      – Kuldeep
      Nov 21 '18 at 12:45











    • Just added the solution 😊

      – Danny182
      Nov 21 '18 at 13:07











    • @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

      – iRiziya
      Nov 29 '18 at 5:49






    • 1





      For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

      – Danny182
      Nov 29 '18 at 11:35











    • @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

      – iRiziya
      Nov 30 '18 at 12:08
















    1














    I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊



    My wrapper is very simple but does what I needed:



    #import <Foundation/Foundation.h>
    #import "WebPWrapper.h"
    #import "UIImage+WebP.h"

    @implementation WebPWrapper
    -(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
    NSData *webPData = [UIImage imageToWebP:image quality:quality];
    return webPData;
    }
    @end


    In swift I use it this way:



    let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)





    share|improve this answer


























    • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

      – Kuldeep
      Nov 21 '18 at 12:45











    • Just added the solution 😊

      – Danny182
      Nov 21 '18 at 13:07











    • @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

      – iRiziya
      Nov 29 '18 at 5:49






    • 1





      For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

      – Danny182
      Nov 29 '18 at 11:35











    • @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

      – iRiziya
      Nov 30 '18 at 12:08














    1












    1








    1







    I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊



    My wrapper is very simple but does what I needed:



    #import <Foundation/Foundation.h>
    #import "WebPWrapper.h"
    #import "UIImage+WebP.h"

    @implementation WebPWrapper
    -(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
    NSData *webPData = [UIImage imageToWebP:image quality:quality];
    return webPData;
    }
    @end


    In swift I use it this way:



    let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)





    share|improve this answer















    I ended up using another framework to convert PNG to WebP: https://github.com/seanooi/iOS-WebP, had to create the wrapper to use it on swift but it works very good 😊



    My wrapper is very simple but does what I needed:



    #import <Foundation/Foundation.h>
    #import "WebPWrapper.h"
    #import "UIImage+WebP.h"

    @implementation WebPWrapper
    -(NSData *)convertUIImageToWebp:(UIImage *)image :(int)quality {
    NSData *webPData = [UIImage imageToWebP:image quality:quality];
    return webPData;
    }
    @end


    In swift I use it this way:



    let webPData = WebPWrapper().convertUIImage(toWebp: image, 90)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 13:02

























    answered Nov 21 '18 at 9:18









    Danny182Danny182

    1,0371219




    1,0371219













    • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

      – Kuldeep
      Nov 21 '18 at 12:45











    • Just added the solution 😊

      – Danny182
      Nov 21 '18 at 13:07











    • @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

      – iRiziya
      Nov 29 '18 at 5:49






    • 1





      For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

      – Danny182
      Nov 29 '18 at 11:35











    • @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

      – iRiziya
      Nov 30 '18 at 12:08



















    • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

      – Kuldeep
      Nov 21 '18 at 12:45











    • Just added the solution 😊

      – Danny182
      Nov 21 '18 at 13:07











    • @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

      – iRiziya
      Nov 29 '18 at 5:49






    • 1





      For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

      – Danny182
      Nov 29 '18 at 11:35











    • @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

      – iRiziya
      Nov 30 '18 at 12:08

















    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Kuldeep
    Nov 21 '18 at 12:45





    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Kuldeep
    Nov 21 '18 at 12:45













    Just added the solution 😊

    – Danny182
    Nov 21 '18 at 13:07





    Just added the solution 😊

    – Danny182
    Nov 21 '18 at 13:07













    @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

    – iRiziya
    Nov 29 '18 at 5:49





    @Danny182 Hello, I tried this but not working properly. I mean it returns nil image when decode from .webp to png image from Data. Can you please share your example so it can help many of us. Struggling since a week, now you are the only hope!

    – iRiziya
    Nov 29 '18 at 5:49




    1




    1





    For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

    – Danny182
    Nov 29 '18 at 11:35





    For this need I used another library even if I’m sure the one that I used here (iOS-webp) does the same but you need to create the function in the wrapper for that need. If you want you could check the library YYImage. I used that library to decode webp image to data

    – Danny182
    Nov 29 '18 at 11:35













    @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

    – iRiziya
    Nov 30 '18 at 12:08





    @Danny182 yeah it helped..Thanks :) Btw..why I get it resultant image with slightly blue shade on it? Any idea? did you face this?

    – iRiziya
    Nov 30 '18 at 12:08













    1














    Swift 4.0



    pod both 'SDWebImage' AND 'SDWebImage/WebP'



    import SDWebImage



    if let image_download = UIImage(data: data) {
    let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
    }





    share|improve this answer






























      1














      Swift 4.0



      pod both 'SDWebImage' AND 'SDWebImage/WebP'



      import SDWebImage



      if let image_download = UIImage(data: data) {
      let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
      }





      share|improve this answer




























        1












        1








        1







        Swift 4.0



        pod both 'SDWebImage' AND 'SDWebImage/WebP'



        import SDWebImage



        if let image_download = UIImage(data: data) {
        let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
        }





        share|improve this answer















        Swift 4.0



        pod both 'SDWebImage' AND 'SDWebImage/WebP'



        import SDWebImage



        if let image_download = UIImage(data: data) {
        let photo:Data = image_download.sd_imageData(as: SDImageFormat.webP)!
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 30 '18 at 9:53









        Ibrahim

        3,38022036




        3,38022036










        answered Dec 18 '18 at 16:45









        DverdugoDverdugo

        2616




        2616






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402823%2fhow-to-convert-a-png-to-a-webp-in-swift%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]