代码之家  ›  专栏  ›  技术社区  ›  Kelvin Lau

从UIImageView获取调整大小的图像

  •  1
  • Kelvin Lau  · 技术社区  · 10 年前

    我需要将图像数据上传到 Parse.com 但我发现我的照片太大了,尺寸不够。NSHipster有一篇关于调整图像大小的文章,代码如下:

    import ImageIO
    
    if let imageSource = CGImageSourceCreateWithURL(self.URL, nil) {
        let options: CFDictionary = [
            kCGImageSourceThumbnailMaxPixelSize: max(size.width, size.height) / 2.0,
            kCGImageSourceCreateThumbnailFromImageIfAbsent: true
        ]
    
        let scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options).flatMap { UIImage(CGImage: $0) }
    }
    

    不幸的是,这段代码无法在Swift 1.2中编译。这个 CFDictionary 无法初始化,并且 flatMap 无法识别方法。

    2 回复  |  直到 10 年前
        1
  •  1
  •   Dharmesh Kheni Steve Rosenberg    10 年前

    以下是调整图像大小的一些功能:

    import UIKit
    
    func RBSquareImageTo(image: UIImage, size: CGSize) -> UIImage {
        return RBResizeImage(RBSquareImage(image), size)
    }
    
    func RBSquareImage(image: UIImage) -> UIImage {
        var originalWidth  = image.size.width
        var originalHeight = image.size.height
    
        var edge: CGFloat
        if originalWidth > originalHeight {
            edge = originalHeight
        } else {
            edge = originalWidth
        }
    
        var posX = (originalWidth  - edge) / 2.0
        var posY = (originalHeight - edge) / 2.0
    
        var cropSquare = CGRectMake(posX, posY, edge, edge)
    
        var imageRef = CGImageCreateWithImageInRect(image.CGImage, cropSquare);
        return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation)
    }
    
    func RBResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
        let size = image.size
    
        let widthRatio  = targetSize.width  / image.size.width
        let heightRatio = targetSize.height / image.size.height
    
        // Figure out what our orientation is, and use that to form the rectangle
        var newSize: CGSize
        if(widthRatio > heightRatio) {
            newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
        } else {
            newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
        }
    
        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRectMake(0, 0, newSize.width, newSize.height)
    
        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        image.drawInRect(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return newImage
    }
    

    从…起 HERE .

        2
  •  0
  •   Chetan Prajapati    10 年前

    这是一个Objective C代码,用于缩放参数给定的图像。

    -(UIImage*)imageWithImage:(UIImage*)image
                  scaledToSize:(CGSize)newSize;
    {
        UIGraphicsBeginImageContext( newSize );
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }