vendredi 4 septembre 2015

How do I pixilate or add a pattern to a stroke in swift

I am working in swift to create an app allowing the user to draw a pattern or add pixelation via touch to an image. I have followed a few tutorials and at this point, I am able to grab an image and draw over it, but now I am stuck. How do I replace a black line with a pattern or pixelated version of the picture in the stroke radius?

import UIKit
import AVFoundation
import Social
import MobileCoreServices
import MessageUI
import iAd
import CoreGraphics

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UITextViewDelegate  {

@IBOutlet weak var mainImageView: UIImageView!
@IBOutlet weak var tempImageView: UIImageView!

let picker = UIImagePickerController()
var cameraUI: UIImagePickerController! = UIImagePickerController()

var lastPoint = CGPoint.zeroPoint
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth: CGFloat = 20.0
var opacity: CGFloat = 1.0
var swiped = false


@IBAction func getImage(sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
    {
        cameraUI = UIImagePickerController()
        cameraUI.delegate = self
        cameraUI.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        cameraUI.mediaTypes = [kUTTypeImage]
        cameraUI.allowsEditing = false

        self.presentViewController(cameraUI, animated: true, completion: nil)
    }
    else
    {
        let alertVC = UIAlertController(title: "Error", message: "Cannot open camera roll.", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style:.Default, handler: nil)
        alertVC.addAction(okAction)
        presentViewController(alertVC, animated: true, completion: nil)
    }

}

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

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
    mainImageView.contentMode = .ScaleAspectFit //3
    mainImageView.image = chosenImage //4
    dismissViewControllerAnimated(true, completion: nil) //5
}

func noCamera(){
    let alertVC = UIAlertController(title: "No Camera", message: "Sorry, this device has no camera", preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style:.Default, handler: nil)
    alertVC.addAction(okAction)
    presentViewController(alertVC, animated: true, completion: nil)
}


func savedImageAlert()
{
    var alert:UIAlertView = UIAlertView()
    alert.title = "Saved!"
    alert.message = "Your picture was saved"
    alert.delegate = self
    alert.addButtonWithTitle("Ok")
    alert.show()
}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    swiped = false
    if let touch = touches.first as? UITouch {
        lastPoint = touch.locationInView(self.view)
    }
}


func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

    // 1
    UIGraphicsBeginImageContext(view.frame.size)
    let context = UIGraphicsGetCurrentContext()
    tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

    // 2
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y)

    // 3
    CGContextSetLineCap(context, kCGLineCapRound)
    CGContextSetLineWidth(context, brushWidth)
    CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
    CGContextSetBlendMode(context, kCGBlendModeNormal)
    CGContextSetInterpolationQuality(context, kCGInterpolationLow)


//    CGContextSetStrokePattern(context, pattern: pattern, components: UnsafePointer<CGFloat>())

    // 4
    CGContextStrokePath(context)

    // 5
    tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    tempImageView.alpha = opacity
    UIGraphicsEndImageContext()

}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    // 6
    swiped = true
    if let touch = touches.first as? UITouch {
        let currentPoint = touch.locationInView(view)
        drawLineFrom(lastPoint, toPoint: currentPoint)

        // 7
        lastPoint = currentPoint
    }
}



override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    if !swiped {
        // draw a single point
        drawLineFrom(lastPoint, toPoint: lastPoint)
    }

    // Merge tempImageView into mainImageView
    UIGraphicsBeginImageContext(mainImageView.frame.size)
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
    tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    tempImageView.image = nil
}
}

Aucun commentaire:

Enregistrer un commentaire