2016-08-17 16 views
2

イメージの配列をイメージの長い行のように見える単一のイメージに結合しようとしています。 は:ここenter image description hereイメージの配列を1つのイメージに結合します。イメージは1つのイメージにまとめられます。

は、この質問にも関連しているだけ

func combine(images: NSMutableArray) -> UIImage { 
    var contextSize = CGSizeZero 
    var xMaxSize : CGFloat = 0 
    for image in images { 
     // 

     xMaxSize = xMaxSize + image.size.width 
     //contextSize.width = max(contextSize.width , image.size.width) 
     contextSize.height = max(contextSize.height, image.size.height) 
    } 
    contextSize.width = xMaxSize 

    UIGraphicsBeginImageContextWithOptions(contextSize, false, UIScreen.mainScreen().scale) 


    var imageXPlace :CGFloat = 0 

    for image in images { 


     let originX = imageXPlace 
     let originY = (contextSize.height - image.size.height)/2 


     image.drawInRect(CGRectMake(originX, originY, image.size.width, image.size.height)) 

     imageXPlace = imageXPlace + image.size.width 
    } 

    let combinedImage = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

    return combinedImage 
} 

現在、シングルサイズの画像のために動作するコードであり、(ノートではイメージがまたは類似のサイズのものであってもなくてもよいです)これまでone

答えて

1
  • 画像のサイズが異なる
  • 合成画像の
  • 高 - UIImage
  • における最小の高さ

ViewController.swift

import UIKit 

struct ImageToCombine { 
var image = UIImage() 

var size: CGSize { 
    get { 
     return CGSize(width: image.size.width, height: image.size.height) 
    } 
} 

var widthAndHeightRatio: CGFloat { 
    get { 
     return image.size.width/image.size.height 
    } 
} 

init (image: UIImage) { 
    self.image = image 
} 
} 

class ViewController: UIViewController { 

var imageView = UIImageView() 
var images = [ImageToCombine]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    imageView = UIImageView(frame: UIScreen.mainScreen().bounds) 
    imageView.contentMode = .ScaleAspectFit 
    view.addSubview(imageView) 

    images.append(ImageToCombine(image: UIImage(named: "image1")!)) 
    images.append(ImageToCombine(image: UIImage(named: "image2")!)) 
    images.append(ImageToCombine(image: UIImage(named: "image3")!)) 

    imageView.image = combine(images, spaceBetweenImages: 10) 
} 

func combine(images: [ImageToCombine], spaceBetweenImages:CGFloat) -> UIImage { 

    var fullWidth:CGFloat = 0 
    var minHeight:CGFloat = images[0].size.height 

    for index in 0..<images.count { 
     fullWidth += images[index].size.width 
     if (index+1 < images.count) { 
      fullWidth += spaceBetweenImages 
     } 
     if (minHeight > images[index].size.height) { 
      minHeight = images[index].size.height 
     } 
    } 

    let quality = CGFloat(4) 
    let boundForBetter = CGRectMake(0, 0, fullWidth*quality, minHeight*quality) 

    UIGraphicsBeginImageContext(boundForBetter.size) 
    let context = UIGraphicsGetCurrentContext() 

    for index in 0..<images.count { 

     var imageX:CGFloat = 0 
     for index2 in 0..<index { 
      imageX += images[index2].size.width 

      if (index2+1 < images.count) { 
       imageX += spaceBetweenImages 
      } 
     } 
     let imageY:CGFloat = (minHeight - images[index].size.height)*quality/2 
     let rect = CGRect(x: imageX*quality, y: imageY, width: images[index].size.width*quality, height: images[index].size.height*quality) 
     CGContextDrawImage(context, rect, images[index].image.CGImage) 
    } 

    let combinedImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    let resultImageimage = UIImage(CGImage: imageResize(combinedImage, sizeChange:CGSize(width: fullWidth/quality, height: minHeight/quality)).CGImage!, scale: 1.0, orientation: .DownMirrored) 

    return resultImageimage 
} 

func imageResize(imageObj:UIImage, sizeChange:CGSize)-> UIImage { 

    let hasAlpha = true 
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen 

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale) 
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange)) 

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() // !!! 
    return scaledImage 
} 
} 

結果:

enter image description here

+1

おかげシーリー、場合Iあなたのコードウィットをテストしましたhは等しいサイズではありませんが、結果は長い画像です(私がすべきである)が、画像は上下逆さまです。何か案が? (あなたの素早い鳥も物の裏側を飛んでいる...) – chewy

+0

私は私の答えを編集しました –

+1

もう一度おかげさまで、あなたの鳥はまだ逆さまです.... – chewy

関連する問題