Created
April 10, 2019 14:07
-
-
Save hindalsayyar/18eda28a6e2e9ed677f491e6896a0f6e to your computer and use it in GitHub Desktop.
Erase/Unerase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import AVFoundation | |
class ClassImageEditingView:UIView{ | |
var context: CGContext? | |
var contextBounds = CGRect.zero | |
var backgroundImage: UIImage? | |
var foregroundImage: UIImage? | |
var touchWidth: CGFloat = 0.0 | |
var touchRevealsImage = false | |
var firstTouch:Bool = true | |
var foregroundImagePicked:UIImage? | |
var scratchable:CGImage? | |
var scratched:CGImage? | |
var location = CGPoint(x: 0, y: 0) | |
var PreviosLocation:CGPoint! | |
// static let sharedInstance : ClassImageEditingView = { | |
// let instance = ClassImageEditingView() | |
// return instance | |
// }() | |
// | |
struct sharedInstance{ | |
static var foregroundImagePicked:UIImage? | |
} | |
//MARK: Create createBitmapContext | |
func createBitmap(){ | |
//setForegroundImage(foregroundImagePicked) | |
scratchable = foregroundImage?.cgImage | |
// let selfRatio: CGFloat = frame.size.width / frame.size.height //change the size later | |
// let imgRatio: CGFloat = (foregroundImage?.size.width ?? 0.0) / (foregroundImage?.size.height ?? 0.0) | |
// | |
// var rect : CGRect | |
// rect = CGRect(x: 0, y: 0, width: 0, height: 0) | |
// | |
// if selfRatio > imgRatio { | |
// // view is wider than img | |
// rect.size.height = frame.size.height | |
// rect.size.width = imgRatio * (rect.size.height ?? 0.0) | |
// } else { | |
// // img is wider than view | |
// rect.size.width = frame.size.width | |
// rect.size.height = (rect.size.width ?? 0.0) / imgRatio | |
// } | |
// | |
// rect.origin.x = 0.5 * (frame.size.width - (rect.size.width ?? 0.0)) | |
// rect.origin.y = 0.5 * (frame.size.height - (rect.size.height ?? 0.0)) | |
let width = (Int)(self.bounds.width)//(rect.width)//(self.frame.width)//scratchable?.width | |
let hight = (Int)(self.bounds.height)//(rect.width)//(self.frame.height) //scratchable?.height | |
self.isOpaque = false | |
let size:Int = width * hight | |
let bytePerPixel = 4 | |
let bitsPerComponent = 8 //scratchable!.bitsPerComponent | |
let bytesPerRow = bytePerPixel*width //scratchable!.bytesPerRow | |
let totalBytes = hight * bytesPerRow | |
contextBounds = bounds | |
// Grayscale color space | |
let colorSpace = CGColorSpaceCreateDeviceGray(); | |
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue) | |
// Create bitmap content with current image size and grayscale colorspace | |
let pixel: CFMutableData = CFDataCreateMutable(nil, width * hight)//width!*hight! | |
context = CGContext(data:CFDataGetMutableBytePtr(pixel), width: width, height: hight, bitsPerComponent: 8, bytesPerRow: width /*Int(contextBounds.size.width)*/, space: colorSpace, bitmapInfo: CGImageAlphaInfo.none.rawValue)// bitmapInfo.rawValue | |
CFDataSetLength(pixel, size) | |
let provider = CGDataProvider(data: pixel) | |
//CGDataProvider(data: pixel!) | |
//make it white (touchRevealsImage==NO) | |
let black = UIColor.black //[1.0, 1,0] | |
context?.setFillColor(black.cgColor) | |
context?.fill(bounds) //contextBounds | |
//set the color of path | |
context?.setStrokeColor(UIColor.black.cgColor) | |
context?.setLineWidth(touchWidth) | |
//setup drawing for that context | |
context?.setLineCap(.round) | |
context?.setLineJoin(.round) | |
let mask = CGImage(maskWidth: width, height: hight, bitsPerComponent: 8, bitsPerPixel: 8, bytesPerRow: width, provider: provider!, decode: nil, shouldInterpolate: false) | |
scratched = scratchable?.masking(mask!) | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
print("begin toich") | |
let touch = event?.touches(for: self)?.first as! UITouch | |
firstTouch = true | |
location = touch.location(in: self) | |
} | |
// Converted to Swift 5 by Swiftify v5.0.29819 - https://objectivec2swift.com/ | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | |
print("move touch") | |
let touch = event?.touches(for: self)?.first | |
// the new line that will be drawn | |
if firstTouch == true { | |
firstTouch = false | |
PreviosLocation = touch?.previousLocation(in: self) | |
}else { | |
location = (touch?.location(in: self))! | |
print(location) | |
PreviosLocation = touch?.previousLocation(in: self) | |
} | |
//change the color of stock of erase or not swift | |
let color = (touchRevealsImage ? UIColor.white.cgColor : UIColor.black.cgColor) | |
context!.setStrokeColor(color) | |
context?.setLineWidth(touchWidth) | |
points(from:PreviosLocation, to:location) | |
} | |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | |
print("End of touch") | |
let touch = event?.touches(for: self)?.first | |
if firstTouch == true { | |
firstTouch = false | |
PreviosLocation = touch?.location(in: self) | |
} | |
points(from:PreviosLocation, to:location) | |
} | |
func points(from:CGPoint, to:CGPoint){ | |
context?.move(to: CGPoint(x: PreviosLocation.x, y: PreviosLocation.y)) | |
context?.addLine(to: CGPoint(x: location.x, y: location.y)) | |
context?.strokePath() | |
setNeedsDisplay() | |
} | |
override func draw(_ rect: CGRect) { | |
print("we are drawing") | |
//this code get you the image in scale fit size | |
let selfRatio: CGFloat = bounds.size.width / bounds.size.height //change the size later | |
let imgRatio: CGFloat = (foregroundImage?.size.width ?? 0.0) / (foregroundImage?.size.height ?? 0.0) | |
var rect : CGRect | |
rect = CGRect(x: 0, y: 0, width: 0, height: 0) | |
if selfRatio > imgRatio { | |
// view is wider than img | |
rect.size.height = bounds.size.height | |
rect.size.width = imgRatio * (rect.size.height ?? 0.0) | |
} else { | |
// img is wider than view | |
rect.size.width = bounds.size.width | |
rect.size.height = (rect.size.width ?? 0.0) / imgRatio | |
} | |
rect.origin.x = 0.5 * (bounds.size.width - (rect.size.width ?? 0.0)) | |
rect.origin.y = 0.5 * (bounds.size.height - (rect.size.height ?? 0.0)) | |
//you have to draw in the right place so it follow your finger moves. | |
UIGraphicsGetCurrentContext()?.saveGState() | |
//UIGraphicsGetCurrentContext()?.translateBy(x: 0, y: rect.height)//self.frame.size.height// | |
//UIGraphicsGetCurrentContext()?.scaleBy(x: 1.0, y: -1.0) | |
UIGraphicsGetCurrentContext()?.draw(scratched!, in: rect) | |
//foregroundImage?.draw(in: rect) | |
UIGraphicsGetCurrentContext()?.restoreGState() | |
} | |
// func drawImageScaled(_ image: UIImage?) { | |
// print("we are scalling the drawing") | |
// // just draws the image scaled down and centered | |
// | |
// let selfRatio: CGFloat = frame.size.width / frame.size.height //change the size later | |
// let imgRatio: CGFloat = (image?.size.width ?? 0.0) / (image?.size.height ?? 0.0) | |
// | |
// var rect : CGRect | |
// rect = CGRect(x: 0, y: 0, width: 0, height: 0) | |
// | |
// if selfRatio > imgRatio { | |
// // view is wider than img | |
// rect.size.height = frame.size.height | |
// rect.size.width = imgRatio * (rect.size.height ?? 0.0) | |
// } else { | |
// // img is wider than view | |
// rect.size.width = frame.size.width | |
// rect.size.height = (rect.size.width ?? 0.0) / imgRatio | |
// } | |
// | |
// rect.origin.x = 0.5 * (frame.size.width - (rect.size.width ?? 0.0)) | |
// rect.origin.y = 0.5 * (frame.size.height - (rect.size.height ?? 0.0)) | |
// | |
// image?.draw(in: rect ?? CGRect.zero) | |
// } | |
// func resetDrawing() { | |
// // draw black or white | |
// //let color = [(touchRevealsImage ? 0.0 : 1.0), 1.0] | |
// let color = (touchRevealsImage ? UIColor.black.cgColor:UIColor.white.cgColor) | |
// //context?.setStrokeColorSpace(color as! CGColorSpace) | |
// | |
// | |
// context!.setFillColor(color) | |
// context!.fill(contextBounds) | |
// | |
// setNeedsDisplay() | |
// } | |
// override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { | |
// return subviews.contains(where: { | |
// !$0.isHidden && $0.point(inside: self.convert(point, to: $0), with: event) | |
// }) | |
// } | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
//fatalError("init(coder:) has not been implemented") | |
createBitmap() | |
touchWidth = 10.0 | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
// createBitmap() | |
touchWidth = 10.0 | |
} | |
func setBackgroundImage(value:UIImage) { | |
if value != backgroundImage { | |
backgroundImage = value | |
setNeedsDisplay() | |
} | |
} | |
func setForegroundImage(_ value: UIImage?) { | |
if value != foregroundImage { | |
foregroundImage = value | |
let TopImageRect = AVMakeRect(aspectRatio: foregroundImage!.size, insideRect: bounds).size | |
print("The size in the class \(TopImageRect)") | |
foregroundImage?.af_imageAspectScaled(toFit:TopImageRect ) | |
setNeedsDisplay() | |
} | |
} | |
func setTouchRevealsImage(_ value: Bool) { | |
if value != touchRevealsImage { | |
touchRevealsImage = value | |
setNeedsDisplay() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment