๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Third Party

[iOS/Library] WebP๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€์šฉ๋Ÿ‰ ์ค„์ด๊ธฐ

by Fomagran ๐Ÿ’ป 2020. 4. 14.
728x90
๋ฐ˜์‘ํ˜•

"WebP"๋Š” ๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  ์ด๋ฏธ์ง€ ํฌ๋ฉง์ž…๋‹ˆ๋‹ค.

ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” GIF,PNG,JPEG ํŒŒ์ผ์„ ์ตœ๋Œ€ 30%๊นŒ์ง€ ์šฉ๋Ÿ‰์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ์•„๋ž˜ ๊นƒํ—ˆ๋ธŒ์ฃผ์†Œ๋กœ ๊ฐ€์‹  ๋‹ค์Œ์—

https://github.com/ainame/Swift-WebP

 

ainame/Swift-WebP

A thin Swift wrapper of libwebp to make your own encoder/decoder app - ainame/Swift-WebP

github.com

์›นํ”ผ๋ฅผ ์„ค์น˜ํ•ด์ฃผ์‹œ๋ฉด ๋˜๋Š”๋ฐ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Cocoapods์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ณ ๋กœ Carthage๋กœ ๋นŒ๋“œํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Carthage๋กœ ๋นŒ๋“œํ•˜๋Š” ๋ฒ•์€ ์•„๋ž˜ ๊ธ€์„ ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”

https://fomaios.tistory.com/220

 

Carthage๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ด€๋ฆฌํ•˜๊ธฐ

Carthage๋Š” CocoaPods์™€ ๊ฐ™์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋” ์ฝ”์ฝ”์•„ํŒŸ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์–ด ์ด๋Ÿด ๋• ์นด๋ฅดํƒ€๊ณ ๋ฅผ ์‚ฌ์šฉํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ ์šฐ์„  brew๋ผ๋Š” MacOS์šฉ ํŒจํ‚ค์ง€๊ด€๋ฆฌ์ž๋ฅผ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค. https..

fomaios.tistory.com

์šฐ์„  ์ฒซ์งธ๋กœ ํ•  ์ผ์€ WebP๋ฅผ importํ•ด์ฃผ์‹œ๊ณ 

๊ทธ ๋‹ค์Œ์€ ์ด๋ฏธ์ง€๋ฅผ ์›นํ”ผ์ด๋ฏธ์ง€๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฒ• ์ž…๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ์›นํ”ผ์ธ์ฝ”๋”๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ฑฐ๊ธฐ์— image๋ฅผ ๋„ฃ์–ด์„œ ์›นํ”ผ๋ฐ์ดํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

1
2
 let encoder = WebPEncoder()
 let webpdata = try! encoder.encode(image!, config: .preset(.picture, quality: 95))

ํ•œ๋ฒˆ ์›๋ž˜ ์ด๋ฏธ์ง€์™€ ์›นํ”ผ์ด๋ฏธ์ง€์™€ ์šฉ๋Ÿ‰์„ ๋น„๊ตํ•ด๋ณผ๊นŒ์š”? ์•„๋ž˜์™€ ๊ฐ™์ด png๋ฐ์ดํ„ฐ์˜ ์šฉ๋Ÿ‰๊ณผ ์›นํ”ผ๋กœ ์ธ์ฝ”๋”ฉํ•œ ๋ฐ์ดํ„ฐ์˜ ์šฉ๋Ÿ‰์„ ์ถœ๋ ฅํ•ด๋ณด๋ฉด

์•„๋ž˜์™€ ๊ฐ™์ด ๊ฑฐ์˜ 4๋ถ„์˜1์˜ ์šฉ๋Ÿ‰์œผ๋กœ ์ค„์—ฌ์กŒ์Šต๋‹ˆ๋‹ค!!!

์ด๋ ‡๊ฒŒ ์›นํ”ผ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ €์žฅํ•ด๋†“์•˜๋‹ค๊ฐ€ ๋‹ค์‹œ ๋””์ฝ”๋”ฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋กœ ์‚ฌ์šฉํ•  ๋•Œ๋Š”

์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ฏธ์กฐ ๋งŒ๋“ค์–ด์ค€ ๋’ค ์›ํ•˜๋Š” ์ด๋ฏธ์ง€๋ทฐ์— ๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
6
let decoder = WebPDecoder()
var options = WebPDecoderOptions()
options.scaledWidth = Int(25//๊ฐ€๋กœํฌ๊ธฐ
options.scaledHeight = Int(25//
์ด๋ฏธ์ง€๋ทฐ.image = UIImage(cgImage: cgImage)
 
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€