Skip to content

Instantly share code, notes, and snippets.

View quangtqag's full-sized avatar
🚀
Researching things that I am lacking...

Quang quangtqag

🚀
Researching things that I am lacking...
View GitHub Profile
let image = UIImage(named: "images.jpeg")!
UIGraphicsBeginImageContextWithOptions(image.size, false, 1.0)
var context = UIGraphicsGetCurrentContext()
//move and invert canvas by scaling
CGContextTranslateCTM(context, 0, image.size.height)
CGContextScaleCTM(context, 1, -1)
image.drawInRect(CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
func grayScale() -> UIImage {
let width = self.size.width
let height = self.size.height
let imageRect:CGRect = CGRectMake(0, 0, width, height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue)
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue)
CGContextDrawImage(context, imageRect, self.CGImage)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Force view follow specific orientation
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
func cgImageFromSampleBuffer(sampleBuffer: CMSampleBuffer) -> CGImage {
let pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!
let ciImage = CIImage(CVPixelBuffer: pixelBuffer)
let context: CIContext = CIContext(options: nil)
let rect = CGRectMake(0, 0, CGFloat(CVPixelBufferGetWidth(pixelBuffer)), CGFloat(CVPixelBufferGetHeight(pixelBuffer)))
let cgImage = context.createCGImage(ciImage, fromRect: rect)
return cgImage
}
func ciImageFromSampleBuffer(sampleBuffer: CMSampleBuffer) -> CIImage {
let pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!
let attachments : CFDictionaryRef = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, pixelBuffer, CMAttachmentMode( kCMAttachmentMode_ShouldPropagate))!
let ciImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments as? [String : AnyObject])
return ciImage
}
class AWSS3Util: NSObject {
class func uploadFileAtURL(fileURL: NSURL,
fileName: String,
progessCallBack: AWSNetworkingUploadProgressBlock?,
responseCallBack: (error: NSError?) -> Void ) {
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.body = fileURL
uploadRequest.key = fileName
//http://blog.apoorvmote.com/uitapgesturerecognizer-to-switch-between-two-uiviews-in-ios-swift/
var isPurpleViewShowing = false
@IBOutlet weak var greenView: UIView!
@IBOutlet weak var purpleView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
extension UIView {
func removeAllConstraints() {
var superview = self.superview
while superview != nil {
for c in superview!.constraints {
if c.firstItem as? UIView == self || c.secondItem as? UIView == self {
superview!.removeConstraint(c)
}
}
superview = superview!.superview
var block: dispatch_block_t?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
block = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS) {
print("I executed")
}
// iOS 9 or Above
// Reference: https://www.raywenderlich.com/125718/coding-auto-layout
//
avatarView.translatesAutoresizingMaskIntoConstraints = false
avatarView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true
avatarView.leadingAnchor.constraintEqualToAnchor(view.layoutMarginsGuide.leadingAnchor).active = true
chapterLabel.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
avatarView.bottomAnchor.constraintEqualToAnchor(chapterLabel.topAnchor,constant: -10).active = true
chapterLabel.setContentHuggingPriority(UILayoutPriorityRequired, forAxis: .Vertical)
chapterLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, forAxis: .Vertical)