Coder Social home page Coder Social logo

uiimageheic's Introduction

UIImageHEIC

Apple introduced widespread HEIC support with iOS 11, but the APIs for it are somewhat low level. This tiny project adds a familiar interface for encoding UIImages into HEIC data similar to what we're used to doing with JPEG or PNG data.

Installation

Add the UIImage+HEIC.h and UIImage+HEIC.m source files to your project. At the moment you must be using Xcode 9 / building with the iOS 11 SDK to use this.

Usage

Converting UIImages to HEIC

This adds a function named tj_UIImageHEICRepresentation that behaves just like UIImageJPEGRepresentation. The method returns nil in the event that HEIC encoding isn't possible on the current device.

So, where you used to have

UIImage *image = /**/;
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);

You could now have

UIImage *image = /**/;
NSData *imageData = tj_UIImageHEICRepresentation(image, 0.8);
if (imageData.length == 0) {
    imageData = UIImageJPEGRepresentation(image, 0.8);
}

UIGraphicsImageRenderer Extensions

This project also adds a category to UIGraphicsImageRenderer for HEIC exporting support with fallbacks to PNG or JPEG. It's used just like you use UIGraphicsImageRenderer's existing PNG and JPEG exporting methods.

Before

UIGraphicsImageRenderer *renderer = /**/;
NSData *data = [renderer PNGDataWithActions:/**/];

After with no fallback

UIGraphicsImageRenderer *renderer = /**/;
NSData *data = [renderer tj_HEICDataWithCompressionQuality:1.0 actions:/**/];

After falling back to PNG

UIGraphicsImageRenderer *renderer = /**/;
NSData *data = [renderer tj_HEICDataFallingBackToPNGDataWithCompressionQuality:1.0 actions:/**/];

After falling back to JPEG

UIGraphicsImageRenderer *renderer = /**/;
NSData *data = [renderer tj_HEICDataWithCompressionQuality:1.0 fallingBackToJPEGDataWithCompressionQuality:1.0 actions:/**/];

Checking HEIC images

You can check if the image at a particular path is a HEIC image using tj_isImageAtPathHEIC on devices that support HEIC reading.

BOOL isHEICImage = tj_isImageAtPathHEIC(/*path to an image*/);

For lower level access you can also use tj_CGImageSourceUTIIsHEIC, which allows you to check using an image source made from data or a URL, and is also helpful if you want to immediately use the image source to perform a transformation in the event it is HEIC.

BOOL isHEICImageSource = tj_CGImageSourceUTIIsHEIC(/*image source*);

uiimageheic's People

Contributors

jonasgessner avatar timonus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

uiimageheic's Issues

Transparency

If your UIImage has transparency you end up with a black background despite HEIC being supposed to support transparency (and the docs saying the default bg color when the format doesn't support transparency is supposed to be white) - any ideas on how to fix this? I've played around for a couple hours and haven't come up with anything.

WebP

iOS 14 has native WebP support, perhaps this library could be expanded to include that as well.

UIImageHEIC

Hi, Support team!
Can I read HEIC format image in macOS10.13 using this source?

When does tj_UIImageHEICRepresentation.length return 0?

Does imageData.length return 0 if HEIC is not supported? When/why would you fallback to JPEG representation?

NSData *imageData = tj_UIImageHEICRepresentation(image, 0.8);
if (imageData.length == 0) {
imageData = UIImageJPEGRepresentation(image, 0.8);
}

Awesome project. Thanks!

Be careful with using exact naming conventions as Apple

Hello,

Cool library!

Be careful with using exact naming conventions as Apple. It is not out of the question that Apple could name a function UIImageHEICRepresentation in the future. At best, there could be a symbol collision or at worst, undefined behavior.

I understand the want to have a similarly named symbols, even if for completionist reasons only, but the risks are not worth it.

导致图片的imageOrientation丢失

视图使用tj_UIImageHEICRepresentation保存为heic格式 ,保存到Document沙盒目录之后, 转换为jpg格式并导出到 系统相册 结果图片是 横向显示的 本来imageOrientation 应该 是2 但是导出的时候的到的 是0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.