troubleshooting Question

Magnifying Glass Effect on Cocoa

Avatar of _the_reverend
_the_reverend asked on
CSmartphonesApple HardwareSwift ProgrammingiPhone
2 Comments1 Solution1816 ViewsLast Modified:
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];

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros