Core Graphics Tutorial: Patterns

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

Core Graphics Tutorial: Patterns

Postby rwenderlich » Fri Apr 19, 2013 11:00 am

This is the official thread to discuss the following blog post: Core Graphics Tutorial: Patterns
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Site Admin
 
Posts: 2503
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 36 times
Been thanked: 472 times

Re: Core Graphics Tutorial: Patterns

Postby thehashblackberry » Tue Jun 24, 2014 6:10 am

Hello Ray,

Hope you are doing good!

I need your help! I am trying to create a mask from a UIImage for matching algorithm. I tried CIFilters also but result is not so satisfactory. Please guide me with a correct approach.

For example, i have an image of Eagle in jpg format , what i want to do is to outline the eagle in that image and fill it with black color, and white color for background.

Regards.
thehashblackberry
n00b
 
Posts: 1
Joined: Thu Aug 23, 2012 5:56 am
Has thanked: 0 time
Been thanked: 0 time

Postby drutt1 » Mon Mar 23, 2015 7:51 pm

Hi, great example in ObjC. I am trying to get this to work in Swift, but cannot get the CGPatternCallbacks code set up to work correctly. Any chance this code has a Swift equivalent somewhere to be easily viewed? Thanks!
drutt1
n00b
 
Posts: 4
Joined: Thu Sep 11, 2014 1:38 pm
Has thanked: 0 time
Been thanked: 0 time

Postby alienMethod » Sun Jan 03, 2016 1:07 am

@drutt1 : The following Swift 2.1 version of CoolPatternView seems to work:


Code: Select all
import UIKit

func radians(degrees:Double) -> CGFloat {
  return CGFloat(degrees * M_PI / 180.0)
}

class CoolPatternView: UIView {
 
  // Only override drawRect: if you perform custom drawing.
  // An empty implementation adversely affects performance during animation.
  override func drawRect(rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
   
    let bgColor = UIColor(hue: 0, saturation: 0, brightness: 0.15, alpha: 1.0)
    CGContextSetFillColorWithColor(context, bgColor.CGColor)
    CGContextFillRect(context, rect)
   
    let callbacks:CGPatternCallbacks = CGPatternCallbacks(
      version: 0,
      drawPattern:  { (pointer, context) -> Void in
        let dotColor = UIColor(hue: 0, saturation: 0, brightness: 0.07, alpha: 1.0)
        let shadowColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.1)
       
        CGContextSetFillColorWithColor(context, dotColor.CGColor);
        CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 1, shadowColor.CGColor);
       
        CGContextAddArc(context, 3, 3, 4, 0, radians(360), 0);
        CGContextFillPath(context);
       
        CGContextAddArc(context, 16, 16, 4, 0, radians(360), 0);
        CGContextFillPath(context);
      },
      releaseInfo: nil
    )
   
    CGContextSaveGState(context)
    let patternSpace = CGColorSpaceCreatePattern(nil)
    CGContextSetFillColorSpace(context, patternSpace)
   
    let pattern = CGPatternCreate(
      nil,
      rect,
      CGAffineTransformIdentity,
      24,
      24,
      .ConstantSpacing,
      true,
      [ callbacks ]
    )
   
    let alpha:CGFloat = 1.0
    CGContextSetFillPattern(context, pattern, [alpha])
    CGContextFillRect(context, self.bounds)
    CGContextRestoreGState(context)
  }
 
}
alienMethod
n00b
 
Posts: 2
Joined: Fri Sep 11, 2015 1:32 am
Has thanked: 0 time
Been thanked: 0 time


Return to Official Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests