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")!
let degrees: Double = 90
let rads = CGFloat(M_PI * degrees / 180)
UIGraphicsBeginImageContext(CGSizeMake(image.size.height, image.size.width))
let ctx = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(ctx, image.size.height/2, image.size.width/2)
CGContextRotateCTM(ctx, rads)
CGContextScaleCTM(ctx, 1.0, -1.0)
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)
func fixOrientation() -> UIImage {
// No-op if the orientation is already correct
if ( self.imageOrientation == UIImageOrientation.Up ) {
return self;
}
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform: CGAffineTransform = CGAffineTransformIdentity
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