2016-03-18 11 views
0

私のシニアデザインプロジェクトのスライディングタイルパズルアプリを作ろうとしています。アプリは、写真を選択したり撮影したりして、画像を9枚(切り取った画像)にカットして配列に格納し、それを削除してランダムな順序で表示することで構成されていますスクリーン。最後に、ユーザはタイルを正しい順序で配置するまでタイルをタッチまたはスライドさせる。スウィフトのiOSスライディングパズル

私はさまざまなコード例をオンラインで見つけましたが、Swiftでは書かれていません。私が見つけた古いコードを解釈し、それをSwiftに書き直そうとすると、私はあらゆる種類のエラーを受け取ります...私はこれをすべて持っている学生初心者です。

私は、CGIizeMake、CGRectMake、およびCGImageCreateWithImageInRectを使用して、displayImageViewに表示されているユーザーが選択したイメージから新しいクロップされたイメージを作成し、それらの新しいクロップされたイメージをtileStack配列に格納しようとしました。しかし、私は何かが不足していると思う...おそらく私は多くを失っている。

ここにはscreenshot of the error when I run itがあります。 ​​

//PhotoViewController.swift Created by Jim on 2/18/16. Copyright © 2016 JamesDphoto.com. All rights reserved. 

import UIKit import Foundation 

class PhotoViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    var tileStack = [AnyObject](); 

    //Beginning of simple image selection and display  
    @IBOutlet weak var displayImageView: UIImageView! 

    @IBAction func choosePicFromLibrary(sender: AnyObject) { 
     let imagePicker: UIImagePickerController = UIImagePickerController() 

     imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
     imagePicker.delegate = self 
     imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover 

     if (imagePicker.popoverPresentationController != nil) { 
      imagePicker.popoverPresentationController!.sourceView = sender as! UIButton 
      imagePicker.popoverPresentationController!.sourceRect = (sender as! UIButton).bounds 
     } 
     presentViewController(imagePicker, animated: true, completion: nil) 
    } 
    @IBAction func takePhoto(sender: AnyObject) { 
     let imagePicker: UIImagePickerController = UIImagePickerController() 

     imagePicker.sourceType = UIImagePickerControllerSourceType.Camera 
     imagePicker.delegate = self 
     imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover 

     if (imagePicker.popoverPresentationController != nil) { 
      imagePicker.popoverPresentationController!.sourceView = sender as! UIButton 
      imagePicker.popoverPresentationController!.sourceRect = (sender as! UIButton).bounds 
     } 
     presentViewController(imagePicker, animated: true, completion: nil) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     dismissViewControllerAnimated(true, completion: nil) 
     displayImageView.image = info[UIImagePickerControllerOriginalImage] as! UIImage! 
    } 

    func imagePickerControllerDidCancel(picker: UIImagePickerController) { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 


    //Beginning of function to cut photo into 9 tiles, then randomize them. 

    //cut into 9 tiles and add to tileStack array 
    @IBAction func randomize(sender: AnyObject) { 

    let tileSize = CGSizeMake(displayImageView.image!.size.width/3, displayImageView.image!.size.height/3) 

     for var rowI = 0; rowI < 3; rowI++ 
     { 
      for var colI = 0; colI < 3; colI++ 
      { 
       let tileRect = CGRectMake(CGFloat(rowI) * tileSize.width, tileSize.height * CGFloat(colI), tileSize.width, tileSize.height) 

       let tileImage = CGImageCreateWithImageInRect(displayImageView.image as! CGImage!, tileRect) 
       tileStack.append(tileImage!) 
      } 
     } 
     //display tiles in order on screen 
    } 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 
+0

あなたはエラーの画像へのリンクを、テキスト形式でエラーを投稿してはならない好むだろう。また、コンソールでメッセージを取得する必要があります(アンラップ時に致命的なエラーが発生しているとは限りません) – nhgrif

+0

一方、CGHectMake、CGSizeMakeなどは必要ありません。コンストラクタを使用してください: 'CGSize :、height:) 'など –

+0

投稿していただきありがとうございます、nhgrif! –

答えて

2

あなたは暗黙的にCGImageにUIImageオブジェクトを変換しようとしています。アプリがクラッシュする理由は、displaImageView.imageがnilではないことがわかっている場合にのみ、これを試してみてください。

let tileImage = CGImageCreateWithImageInRect(displayImageView.image!.CGImage, tileRect) 

私は

if let dpimage = displayImageView.image 
{ 
let tileImage = CGImageCreateWithImageInRect(dpimage.CGImage, tileRect) 
} 
+0

ありがとう、shinoys222!私はこれを試してみましょう。 –

+0

素晴らしい!それはエラーのおかげで - ありがとう! –

+0

新しいコードで新しい質問を投稿しました。このコメントセクションには文字数制限があるのでこのスレッドで続けることができるかどうかはわかりませんでした。見てみると素晴らしいかもしれません! –

関連する問題