2016-07-15 5 views
0

私は、カメラから写真を撮ってUIImageViewに配置するアプリを持っています。あなたがUIImageView(私のコード内のtempImageView)にサブビューとして追加されている小さな "クリップアート"ピクチャを追加することができます。カメラロールにサブビューを含むUIImageViewを保存するにはどうすればよいですか?

しかし、私はtempImageView.imageによって画像をカメラに保存しようとすると、画像がさらに大きくなり、それに追加されたサブビューも表示されません。私のサブビューでUIImageViewをカメラロールに保存する方法はありますか?ここで

は、私は、画像を保存する方法である:

@IBAction func saveImageButtonPressed(sender: UIButton) { 

    UIImageWriteToSavedPhotosAlbum(tempImageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil) 
} 

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { 
    if error == nil { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } else { 
     let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } 
} 

そして、ここでは、私は私のtempImageViewに画像を追加する方法である:任意の助け

@IBAction func berlockButtonPressed(sender: UIButton) { 
    let imageName = "berlock.png" 
    let image = UIImage(named: imageName) 
    let imageView = UIImageView(image: image!) 
    imageView.frame = CGRect(x: 200, y: 200, width: 60, height: 100) 

    tempImageView.addSubview(imageView) 
} 

感謝。

答えて

2

画像と画像ビューのサブビューを画像コンテキストで描画し、そのコンテキストを「撮影」する必要があります。私はこのコードをテストしていませんが、これは、あなたが開始されます:

// Create the image context to draw in 
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, UIScreen.mainScreen().scale) 

// Get that context 
let context = UIGraphicsGetCurrentContext() 

// Draw the image view in the context 
imageView.layer.renderInContext(context!) 

// You may or may not need to repeat the above with the imageView's subviews 
// Then you grab the "screenshot" of the context 
let image = UIGraphicsGetImageFromCurrentImageContext() 

// Be sure to end the context 
UIGraphicsEndImageContext() 

// Finally, save the image 
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil) 
+0

ありがとうございました!これはトリックでした、誰かが不思議なら、私はtempImageViewsサブビューのために繰り返す必要はありませんでした。 – nullforlife

0

あなたが好きな画像に何かを描画する必要があり、あなたがあなたの希望のサイズの代わりに、imageView.bounds.sizeを与えることができます

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.opaque, 0.0) 

    imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!) 

    let resultImageToStore = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

imageView.bounds.sizeあなたのイメージビューのサイズを保つ。

別のサブビューを持つ画像ビューとしてimageViewを考えてみましょう。

resultImageToStoreは、保存する必要がある最終画像です。

+0

ありがとう、これはおそらく私が見ることができますが、少し説明的だったkeithbhunterのソリューションを使用しても同様に動作します。 – nullforlife

+0

あなたはようこそ.... :) – Lion

関連する問題