Difference between revisions of "Swift code snippets"

From Hawk Wiki
Jump to: navigation, search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Swift Code snippts=
+
=Swift Code Snippts=
 
Back To [[IOS_Swift]]
 
Back To [[IOS_Swift]]
 
==[[Swift: Use NSUserDefaults to Store Persistence Data]]==
 
==[[Swift: Use NSUserDefaults to Store Persistence Data]]==
Line 8: Line 8:
  
 
==[[Swift:Using UIRefreshControl]]==
 
==[[Swift:Using UIRefreshControl]]==
 +
 +
==[[Swift:Using UIGestureRecognizer]]==
  
 
==Swift:Example of fetching messages from Parse==
 
==Swift:Example of fetching messages from Parse==
Line 22: Line 24:
 
==[[Swift:UIGraphics Drawing on UIView]]==
 
==[[Swift:UIGraphics Drawing on UIView]]==
  
==Swift: Scale down CGImage==
+
==[[Swift:UIGraphics Scale Down CGImage]]==
<pre class="brush:swift">
+
func scaleDownCGImage(image: CGImage, scale: Float) -> CGImage!{
+
    var scaleDiv = UInt(1.0 / scale)
+
    let width = CGImageGetWidth(image) / scaleDiv
+
    let height = CGImageGetHeight(image) / scaleDiv
+
    let bitsPerComponent = CGImageGetBitsPerComponent(image)
+
    let bytesPerRow = CGImageGetBytesPerRow(image)
+
    let colorSpace = CGImageGetColorSpace(image)
+
    let bitmapInfo = CGImageGetBitmapInfo(image)
+
    let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
+
    CGContextSetInterpolationQuality(context, kCGInterpolationMedium)
+
    let imgSize = CGSize(width: Int(width), height: Int(height))
+
    CGContextDrawImage(context, CGRect(origin: CGPointZero, size: imgSize), image)
+
    return CGBitmapContextCreateImage(context)
+
}
+
</pre>
+
 
+
 
+
==Swift: Convert between CGImage, CIImage and UIImage==
+
===Swift: Convert CIImage to CGImage===
+
<pre class="brush:swift">
+
func convertCIImageToCGImage(inputImage: CIImage) -> CGImage! {
+
    let context = CIContext(options: nil)
+
    if context != nil {
+
        return context.createCGImage(inputImage, fromRect: inputImage.extent())
+
    }
+
    return nil
+
}
+
</pre>
+
===Swift: Convert CGImage to CIImage===
+
<pre class="brush:swift">
+
func convertCGImageToCIImage(inputImage: CGImage) -> CIImage! {
+
  var ciImage = CIImage(CGImage: inputImage)
+
    return ciImage
+
}
+
</pre>
+
===Swift: Convert CGImage or CIImage to UIImage===
+
<pre class="brush:swift">
+
UIImage(CGImage: cgImage)
+
UIImage(CIImage: ciImage)
+
</pre>
+
===Swift: Convert UIImage to CIImage===
+
<pre class="brush:swift">
+
//uiImage: UIImage
+
//Convert to CIImage
+
var ciImage = CIImage(image: uiImage)
+
</pre>
+
===Swift: Convert UIImage to CGImage===
+
To convert to CGImage, it requires 2 steps:
+
 
+
First convert to CIImage [[#Swift: Convert UIImage to CIImage]].
+
 
+
Then convert to CGImage [[#Swift:_Convert_CIImage_to_CGImage]]
+
 
+
==Swift Detect Volumn Button Press==
+
Credit to http://stackoverflow.com/questions/28471481/swift-detect-volume-button-press
+
 
+
Detect volume button press, also hide volume HUD
+
 
+
<pre class="brush:swift">
+
import MediaPlayer //Only for hidding  Volume view
+
func listenVolumeButton(){
+
  
    let audioSession = AVAudioSession.sharedInstance()
+
==[[Swift: Convert between CGImage, CIImage and UIImage]]==
    audioSession.setActive(true, error: nil)
+
    audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)
+
    //If you want to hide Volume HUD view
+
    var volumeView: MPVolumeView = MPVolumeView(frame: CGRectZero)
+
    view.addSubview(volumeView)
+
}
+
  
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject,
+
==[[Swift:Detect Volumn Button Press]]==
    change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
+
    if keyPath == "outputVolume"{
+
        print("got in here")
+
    }
+
}
+
  
</pre>
+
==[[Swift:NSTimer scheduledTimerWithTimeInterval]]==

Latest revision as of 03:10, 9 April 2015

Swift Code Snippts

Back To IOS_Swift

Swift: Use NSUserDefaults to Store Persistence Data

Swift: Making Network Request Using NSURLConnection

Swift:Use AFNetworking setImageWithURL

Swift:Using UIRefreshControl

Swift:Using UIGestureRecognizer

Swift:Example of fetching messages from Parse

https://gist.github.com/sandofsky/7134b1ff90d235901254

Swift:Auto Table Row Height

Swift:Navigation between storyboards

Swift:Notification Observer Using NSNotificationCenter

Swift:Get Raw Histogram from CGImage

Swift:UIGraphics Drawing on UIView

Swift:UIGraphics Scale Down CGImage

Swift: Convert between CGImage, CIImage and UIImage

Swift:Detect Volumn Button Press

Swift:NSTimer scheduledTimerWithTimeInterval