2016-04-22 7 views
0

こんにちは、私はリンゴの鉛筆を使用しているアプリで作業しているので、周りに多くのリソースがありませんので、ここの誰かが助けてくれるでしょう。Save/Retrieve User drawingコアグラフィックス&スウィフト

私のアプリは、アップルの鉛筆、UIImageビュー、コアグラフィックなどを使ってコンテキストを作成することができます。私は今、ユーザーがNSDataを使用して理想的に描画したものを保存できるようにしたいと思いますが、PNGとして保存されているので、保存したファイルを読み込んでUIImage画面に再び表示できますか?

ありがとうございます!

ここに私のコアグラフィックスコードです:

let π = CGFloat(M_PI) 

class NoteCanvasView: UIImageView { 

private var defaultLineWidth:CGFloat = 1 

private var drawColor: UIColor = UIColor.blackColor() 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    guard let touch = touches.first else { return } 

    UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0) 
    let context = UIGraphicsGetCurrentContext() 

    // Draw previous image into context 
    image?.drawInRect(bounds) 

    drawStroke(context, touch: touch) 

    // Update image 
    image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 

private func drawStroke(context: CGContext?, touch: UITouch) { 
    let previousLocation = touch.previousLocationInView(self) 
    let location = touch.locationInView(self) 

    // Calculate line width for drawing stroke 
    let lineWidth = lineWidthForDrawing(context, touch: touch) 

    // Set color 
    drawColor.setStroke() 

    // Configure line 
    CGContextSetLineWidth(context, lineWidth) 
    CGContextSetLineCap(context, .Round) 


    // Set up the points 
    CGContextMoveToPoint(context, previousLocation.x, previousLocation.y) 
    CGContextAddLineToPoint(context, location.x, location.y) 
    // Draw the stroke 
    CGContextStrokePath(context) 

} 

private func lineWidthForDrawing(context: CGContext?, touch: UITouch) -> CGFloat { 

    var lineWidth = defaultLineWidth 

    return lineWidth 
} 
func saveNote(){ 
    let data = UIImagePNGRepresentation(image!) 
    let userDefaults = NSUserDefaults.standardUserDefaults() 
    userDefaults.setObject(data, forKey: "test123") 
    userDefaults.synchronize() 

} 
func retrieveNote(){ 
    if let data = NSUserDefaults.standardUserDefaults().dataForKey("test123"),let image = UIImage(data: data){ 

    } 

} 

func setStrokeColor(color: UIColor){ 
    drawColor = color 
} 
func setStrokeWidth(size: CGFloat){ 
    defaultLineWidth = size 
} 

func clearCanvas(animated animated: Bool) { 
    if animated { 
     UIView.animateWithDuration(0.2, animations: { 
      self.alpha = 0 
      }, completion: { finished in 
       self.alpha = 1 
       self.image = nil 
     }) 
    } else { 
     image = nil 
    } 
} 

}

答えて

0

UIImage(data:imageData,scale:1.0) 

UIImage参照を取ると、このように画像を取得し、表示することができ、同様にあなたが戻ってデータから画像を得たことができますそのイメージはあなたのものですimageview

hこれは役に立ちます:

+0

imageviewで私のUIImageViewを意味しますか?なぜなら、私がそれをバックグラウンドイメージにしようとすると、それは表示されません。 –

+0

はい、 'UIImageview'です。例えば、myImgVuew.image – Lion

+0

うわー、それはうまくいった!私が尋ねていたのは、ストーリーボードのイメージをUIImageViewに設定しようとしたときにエディタに表示されたが、実行したときには表示されなかったからです。助けを感謝します! –

0

あなたはそれを保存し、NSDataとして取り出すことができます。私はまだスケッチを扱う最良の方法が何であるかわからないので、NSDataも考えています。それを試してあなたに知らせるでしょう。