标签:
http://www.cnblogs.com/dinghing154/archive/2012/08/05/2623970.html
在编写程序的时候我们常常使用self.scale来让我们使用的图片放大和缩小,但这种变化并没有真正意义上让我们的图片的大小发生改变,当我们要对这种图片进行点击或其他操作的时候常常会发生错误,这里我使用另外一种方法实现这个效果,完成真正意义上的图片放大和缩小。
这里附上主要代码:
1 - (void) onItem2:(id)sender 2 { 3 CGSize size = _sprt2.contentSize; 4 NSLog(@"\n before modified: image2: width:%f height:%f", size.width, size.height); 5 CCRenderTexture *aRenderTexture = [CCRenderTexture renderTextureWithWidth:1024 height:768]; 6 [aRenderTexture begin]; 7 [self visit]; 8 [aRenderTexture end]; 9 CCSprite *sp = [CCSprite spriteWithTexture:aRenderTexture.sprite.texture]; 10 [sp setFlipY:YES]; 11 [self removeAllChildrenWithCleanup:YES]; 12 13 UIImage *image = [self convertSpriteToImage:sp]; 14 UIImage *newImage = [self scaleFromImage:image toSize:CGSizeMake(512, 384)]; 15 NSData *data = UIImagePNGRepresentation(newImage); 16 [data writeToFile:@"lll.png" atomically:YES]; 17 18 CCSprite *tp = [self convertDataToSprite:data]; 19 [self addChild:tp]; 20 [tp setPosition:ccp(512, 384)]; 21 }
这部分使我们对一张图片纹理进行操作。
接下来我们就对精灵进行放大(缩小)操作,并将处理后的精灵存储。实现真正意义上的图片大小的改变。
1 //CCSprite转换为UIImage 2 - (UIImage *) convertSpriteToImage:(CCSprite *)sprt 3 { 4 CCRenderTexture *render = [CCRenderTexture renderTextureWithWidth:[sprt contentSize].width 5 height:[sprt contentSize].height]; 6 [render begin]; 7 [sprt visit]; 8 [render end]; 9 return [UIImage imageWithData:[render getUIImageAsDataFromBuffer:kCCImageFormatPNG]]; 10 } 11 12 //UIImage放大(缩小)到指定大小 13 - (UIImage *) scaleFromImage:(UIImage *)image toSize:(CGSize)size 14 { 15 UIGraphicsBeginImageContext(size); 16 [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 17 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 18 UIGraphicsEndImageContext(); 19 return newImage; 20 } 21 //UIImage转化为NSData 22 - (NSData *) convertUIImageToData:(UIImage *)image 23 { 24 return UIImagePNGRepresentation(image); 25 }
标签:
原文地址:http://www.cnblogs.com/xuejinhui/p/4330373.html