• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1698
  • Last Modified:

Magnifying Glass Effect on Cocoa

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];

  • 2
1 Solution
_the_reverendAuthor Commented:
I haber round a solution , i will post it tomorrow
thank you!
_the_reverendAuthor Commented:
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];

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now