Skip to content

Commit

Permalink
YJImageIO -> 获取uiimage缩略图
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjun committed Jan 15, 2016
1 parent 07ade82 commit 54c6566
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 15 additions & 0 deletions YJImageIO/YJImageIO/ViewController.swift
Expand Up @@ -16,6 +16,21 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if var image = UIImage(named: "Image") {
if var data = UIImageJPEGRepresentation(image, 1.0) {
print(data.length)
data = UIImageJPEGRepresentation(UIImage(named: "Image")!, 0)!
print(data.length)
image = UIImage(data: data)!
data = UIImagePNGRepresentation(UIImage(named: "Image")!)!
print(data.length)
image = UIImage(data: data)!
image = YJUtilsImageIO.createThumbnailImageFromImage(image)!
}
let imageView = UIImageView(image: image)
imageView.frame = self.view.frame
self.view.addSubview(imageView)
}
}

override func didReceiveMemoryWarning() {
Expand Down
24 changes: 18 additions & 6 deletions YJImageIO/YJImageIO/YJUtilsImageIO.swift
@@ -1,6 +1,6 @@
//
// YJUtils.swift
// YJUtilsImageIO
// YJUtilsImageIO.swift
// YJImageIO
//
// CSDN:http://blog.csdn.net/y550918116j
// GitHub:https://github.com/937447974/Blog
Expand All @@ -15,11 +15,23 @@ import ImageIO
/// ImageIO库工具类
public struct YJUtilsImageIO {

// MARK: -
/// 获取UIImage的缩略图
static func createThumbnailImageFromImage(image: UIImage) -> UIImage? {
guard let data = UIImagePNGRepresentation(image) else {
return nil
}
guard let cgImage = YJUtilsImageIO.createThumbnailImageFromData(data, imageSize: data.length) else {
return nil
}
return UIImage(CGImage: cgImage)
}

/// 获取UIImage的缩略图
static func createThumbnailImageFromData(data: NSData, var imageSize: Int) -> CGImageRef? {
// Create an image source from NSData; no options.
let imageSource = CGImageSourceCreateWithData(data as CFDataRef, nil)
// Make sure the image source exists before continuing.
guard imageSource != nil else {
guard let imageSource = CGImageSourceCreateWithData(data as CFDataRef, nil) else {
print("Image source is NULL.")
return nil
}
Expand All @@ -32,15 +44,15 @@ public struct YJUtilsImageIO {
var vcBacks = kCFTypeDictionaryValueCallBacks
let options: CFDictionaryRef = CFDictionaryCreate(kCFAllocatorDefault, UnsafeMutablePointer(UnsafePointer<Void>(keys)), UnsafeMutablePointer(UnsafePointer<Void>(values)), 2, &kcBacks, &vcBacks)
// Create the thumbnail image using the specified options.
let thumbnailImage = CGImageSourceCreateThumbnailAtIndex(imageSource!, 0, options)
let thumbnailImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)
// Release the options dictionary and the image source, when you no longer need them.
/* swift 废弃
CFRelease(thumbnailSize)
CFRelease(options)
CFRelease(imageSource)
*/
// Make sure the thumbnail image exists before continuing.
guard thumbnailImage == nil else {
guard thumbnailImage != nil else {
print("Thumbnail image not created from image source.")
return nil
}
Expand Down

0 comments on commit 54c6566

Please sign in to comment.