我正在尝试为我的高级设计项目制作滑动拼图应用程序。该应用程序包括选择或拍摄照片(我已经想到),然后让应用程序将图像切割成9件(裁剪图像)并将它们存储在一个数组中,然后删除一个,并以随机顺序显示它们屏幕。最后,用户触摸或滑动瓷砖,直到它们以正确的顺序排列。
我在网上找到了各种代码示例,但没有用Swift编写。当我试图解释我发现的旧代码并在Swift中重新编写代码时,我会遇到各种错误......我是一名学生新手。
我一直在尝试使用CGSizeMake,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 :(得分:2)
您正在尝试隐式地将UIImage对象转换为CGImage。这就是你的应用程序崩溃的原因,只有当你确定displaImageView.image不是nil时才尝试这个。
let tileImage = CGImageCreateWithImageInRect(displayImageView.image!.CGImage, tileRect)
我更喜欢
if let dpimage = displayImageView.image
{
let tileImage = CGImageCreateWithImageInRect(dpimage.CGImage, tileRect)
}