I'm reposting this in advanced as no one was able to answer my question in the regular forum.
In my app I am doing some custom drawing in the touchesMoved method. The code to do this works, as I've tested it on the simulator. The problem is that I have an admob ad on the same view which seems to be preventing the image view that the drawing is being displayed on from updating until touchesEnded, but only on the device. I know the code is still being executed though as log statements still output, and I'm pretty sure that it is the ad causing the problems as when I turn of internet to prevent the ad from loading, everything works perfectly.
I've spent all day trying to figure out why this is happening and its been infuriating having to spend so much time on it. If anyone knows what may be causing this it would be very helpful.
Here's simple code that reproduces the problem:
Note that this only happens on the device, NOT in the
simulator.
.h
Code:
#import <UIKit/UIKit.h>
#import "GADBannerView.h"
@interface admobProblemsViewController : UIViewController
{
UIImageView *imageView;
UIImage *backgroundImage;
UIImage *drawImage;
CGPoint lastPoint;
GADBannerView *banner;
}
@property (retain, nonatomic) UIImageView *imageView;
@property (retain, nonatomic) UIImage *backgroundImage;
@property (retain, nonatomic) UIImage *drawImage;
- (UIImage *)overLayImage:(UIImage *)image1 onImage:(UIImage
*)image2;
@end
.m
Code:
#import "admobProblemsViewController.h"
#import "GADBannerView.h"
@implementation admobProblemsViewController
@synthesize imageView;
@synthesize backgroundImage;
@synthesize drawImage;
- (void)viewDidLoad
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 410)];
drawImage = [[UIImage alloc] init];
backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"jpg"]];
[imageView setImage:backgroundImage];
[self.view addSubview:imageView];
banner = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,
self.view.frame.size.height-GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
banner.adUnitID = @"a14d989945465d2";
banner.rootViewController = self;
[self.view addSubview:banner];
[banner loadRequest:[GADRequest request]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([touch locationInView:self.view].y>=0 && [touch locationInView:self.view].y<=410)
lastPoint = [touch locationInView:imageView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if(currentPoint.y>=0 && currentPoint.y<=410)
{
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
UIImage *temp = [[UIImage alloc] initWithCGImage:[self overLayImage:UIGraphicsGetImageFromCurrentImageContext() onImage:drawImage].CGImage];
UIGraphicsEndImageContext();
[drawImage release];
drawImage = [[UIImage alloc] initWithCGImage:temp.CGImage];
[temp release];
[imageView setImage:[self overLayImage:drawImage onImage:backgroundImage]];
lastPoint = currentPoint;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
UIImage *temp = [[UIImage alloc] initWithCGImage:[self overLayImage:UIGraphicsGetImageFromCurrentImageContext() onImage:drawImage].CGImage];
UIGraphicsEndImageContext();
[drawImage release];
drawImage = [[UIImage alloc] initWithCGImage:temp.CGImage];
[temp release];
[imageView setImage:[self overLayImage:drawImage onImage:backgroundImage]];
}
- (UIImage *)overLayImage:(UIImage *)image1 onImage:(UIImage *)image2
{
UIGraphicsBeginImageContext(image1.size);
[image2 drawAtPoint:CGPointZero];
[image1 drawAtPoint:CGPointZero];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
- (void)dealloc
{
[imageView release];
[backgroundImage release];
[drawImage release];
[super dealloc];
}
@end