diff --git a/core-graphics/src/image.rs b/core-graphics/src/image.rs index 3217335..84945e8 100644 --- a/core-graphics/src/image.rs +++ b/core-graphics/src/image.rs @@ -5,6 +5,7 @@ use core_foundation::base::{CFRetain, CFTypeID}; use core_foundation::data::CFData; use color_space::CGColorSpace; use data_provider::{CGDataProviderRef, CGDataProvider}; +use geometry::CGRect; use libc::size_t; use foreign_types::{ForeignType, ForeignTypeRef}; @@ -121,6 +122,17 @@ impl CGImageRef { }; data_provider.copy_data() } + + /// Returns a cropped image. If the `rect` specifies a rectangle which lies outside of the + /// image bounds, the `None` is returned. + pub fn cropped(&self, rect: CGRect) -> Option { + let image_ptr = unsafe { CGImageCreateWithImageInRect(self.as_ptr(), rect) }; + if !image_ptr.is_null() { + Some(unsafe { CGImage::from_ptr(image_ptr) }) + } else { + None + } + } } #[link(name = "CoreGraphics", kind = "framework")] @@ -146,6 +158,7 @@ extern { shouldInterpolate: bool, intent: u32) -> ::sys::CGImageRef; + fn CGImageCreateWithImageInRect(image: ::sys::CGImageRef, rect: CGRect) -> ::sys::CGImageRef; //fn CGImageGetAlphaInfo(image: ::sys::CGImageRef) -> CGImageAlphaInfo; //fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: ::sys::CGColorSpaceRef) -> ::sys::CGImageRef