Convert Gif image to NSData

后端 未结 4 570
后悔当初
后悔当初 2021-01-17 13:57

I have a gif image in my photo album. When I use the UIImagePickerController to select that image, I need to convert the image to NSData for storin

相关标签:
4条回答
  • 2021-01-17 14:25

    Swift

    Gif must not be in assets.

    let path = Bundle.main.path(forResource: "loader", ofType: "gif")!
    let data = try! Data(contentsOf: URL(fileURLWithPath: path))
    return data
    
    0 讨论(0)
  • 2021-01-17 14:27

    https://github.com/mattt/AnimatedGIFImageSerialization

    UIImage *image = ...;
    NSData *data = [AnimatedGIFImageSerialization animatedGIFDataWithImage:image
                                                                  duration:1.0
                                                                 loopCount:1
                                                                     error:nil];
    
    0 讨论(0)
  • 2021-01-17 14:28

    Code to coverting .GIF file can converted in NSdata -

    NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"myGif" ofType: @"gif"];
    
    NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
    
    NSLog(@"Data: %@", dataOfGif);
    
    0 讨论(0)
  • 2021-01-17 14:38

    The key here is to save the GIF file or URL download directly into a NSData instead of making it a UIImage. Bypassing UIImage will let the GIF file keep the animation.

    Here is some code to convert a GIF file into NSData:

    NSString *filePath = [[NSBundle mainBundle] pathForResource: @"gifFileName" ofType: @"gif"];
    
    NSData *gifData = [NSData dataWithContentsOfFile: filePath];
    

    But in all honesty, you should really consider not using GIF at all.

    0 讨论(0)
提交回复
热议问题