首先,我将图像保存到文档目录
- (NSString *)saveImage {
NSURL *url = [NSURL URLWithString:@"http://ads.buzzcity.net/show.php?partnerid=8404&browser=app_apple"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
//convert image into .png format.
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"myImage"];
[imageData writeToFile:fullPath atomically:YES];
NSLog(@"image saved");
return fullPath;
}
然后我将该路径添加到**itemFromNormalImage:fullPath**
NSString *fullPath=[self saveImage];
btnAD = [CCMenuItemImage itemFromNormalImage:fullPath selectedImage:fullPath target:self selector:@selector(AdButtonAction)];
NSLog(@"btnAD %@", btnAD);
CCMenu *adMenu = [CCMenu menuWithItems:btnAD, nil];
[self addChild:adMenu];
adMenu.position = ccp(350 ,size.height-50);
最后在按钮操作上打开网址
-(void)AdButtonAction
{
NSURL *url = [NSURL URLWithString:@"http://click.buzzcity.net/click.php?partnerid=8404&browser=app_apple"];
NSLog(@"url = %@",url);
[[UIApplication sharedApplication] openURL:url];
}