# Compressing Images for PDFKit

**URL:** <https://forums.swift.org/t/compressing-images-for-pdfkit/44679>\
**Category:** Using Swift\
**Tags:** swiftui, pdfkit, image-compression\
**Created:** [February 11, 2021, 5:51pm UTC](https://forums.swift.org/t/compressing-images-for-pdfkit/44679 "2021-02-11T17:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cj.whitaker](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@cj.whitaker](https://forums.swift.org/u/cj.whitaker)\
**Post date:** [February 11, 2021, 5:51pm UTC](https://forums.swift.org/t/compressing-images-for-pdfkit/44679/1 "2021-02-11T17:51:55Z")

</div>

```swift
NSData *jpegData = UIImageJPEGRepresentation(sourceImage, 0.75);
CGDataProviderRef dp = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);
CGImageRef cgImage = CGImageCreateWithJPEGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);
[[UIImage imageWithCGImage:cgImage] drawInRect:drawRect];

```

Can someone please translate this to **Swift 5** for me?

I'm trying to figure out how to get PDFKit to print the ResizedImage and not the RAW image. I think this may be the solution to my problem, but it's outdated and in another language... I think. I'm a beginner.

Thanks!

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [February 12, 2021, 8:20am UTC](https://forums.swift.org/t/compressing-images-for-pdfkit/44679/2 "2021-02-12T08:20:43Z")

</div>

Here you go:

```swift
import UIKit

let sourceImage: UIImage = ...
let drawRect: CGRect = ...

guard let jpegData = sourceImage.jpegData(compressionQuality: 0.75),
      let dp = CGDataProvider(data: jpegData as CFData),
      let cgImage = CGImage(jpegDataProviderSource: dp, decode: nil, shouldInterpolate: true, intent: .defaultIntent) else {
  // TODO: Handle errors appropriately
  fatalError("Some operation failed")
}
UIImage(cgImage: cgImage).draw(in: drawRect)

```
