Magnifying Glass Effect on Cocoa

Posted on 2009-12-24
Last Modified: 2013-11-25
Hello, can someone help   port this code I found on the web from iphone to mac os ?
Or at least tell how can I get an Image from the current graphic context?
Thank you!
//  CustomView.m
//  MaskTest
//  Created by Sean Christmann on 12/22/08.
//  Copyright 2008 EffectiveUI. All rights reserved.

#import "MagnifierView.h"

@implementation MagnifierView
@synthesize viewref, touchPoint;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
            self.backgroundColor = [UIColor clearColor];
    return self;

- (void)drawRect:(CGRect)rect {
      printf("draw magnifying glass\n");
      if(cachedImage == nil){
            [self.viewref.layer renderInContext:UIGraphicsGetCurrentContext()];
            cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
      CGImageRef imageRef = [cachedImage CGImage];
      CGImageRef maskRef = [[UIImage imageNamed:@"loopmask.png"] CGImage];
      CGImageRef overlay = [[UIImage imageNamed:@"loop.png"] CGImage];
      CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
      //Create Mask
      CGImageRef subImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(touchPoint.x-18, touchPoint.y-18, 36, 36));
      CGImageRef xMaskedImage = CGImageCreateWithMask(subImage, mask);
      // Draw the image
      // Retrieve the graphics context
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGAffineTransform xform = CGAffineTransformMake(
                                                                              1.0,  0.0,
                                                                              0.0, -1.0,
                                                                              0.0,  0.0);
      CGContextConcatCTM(context, xform);
      CGRect area = CGRectMake(touchPoint.x-42, -touchPoint.y, 85, 85);
      CGRect area2 = CGRectMake(touchPoint.x-40, -touchPoint.y+2, 80, 80);
      CGContextDrawImage(context, area2, xMaskedImage);
      CGContextDrawImage(context, area, overlay);

- (void)dealloc {
      [cachedImage release];
      [viewref release];
    [super dealloc];

Question by:_the_reverend

    Author Comment

    I haber round a solution , i will post it tomorrow
    thank you!

    Accepted Solution

    This is how I get an image of an NSView
    First Initialize an NSBitmapImageRep with "initWithFocusedViewRect" and then add the representation to an NSImage.

    If the view is not in focus, you need to call first [theView focus]

    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[theView bounds]];
          NSImage* image = [[NSImage alloc] init];
          [image addRepresentation:rep];

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Learn new improvements released by Google for Google Calendar. Noted in this article are simple tips and tricks that can make your everyday use of Google Calendar better.
    With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices. Find out how to create a mobile email signature with this guide.
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now