<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: Sprite initwithCGImage bug in 0.8.2 issue #349</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/4227</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 01:48:04 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/4227" rel="self" type="application/rss+xml" />

		<item>
			<title>riq on "Sprite initwithCGImage bug in 0.8.2 issue #349"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4227#post-25361</link>
			<pubDate>Wed, 03 Feb 2010 02:45:41 +0000</pubDate>
			<dc:creator>riq</dc:creator>
			<guid isPermaLink="false">25361@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Yes, if you remove the "if", then the cache will return a new instance each time.</p>
<p>issue #<a href='http://www.cocos2d-iphone.org/forum/tags/349'>349</a> reopened.
</p></description>
		</item>
		<item>
			<title>oscarmk on "Sprite initwithCGImage bug in 0.8.2 issue #349"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4227#post-25330</link>
			<pubDate>Tue, 02 Feb 2010 22:45:30 +0000</pubDate>
			<dc:creator>oscarmk</dc:creator>
			<guid isPermaLink="false">25330@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hello,</p>
<p>I am creating a bunch of Sprites using initwithCGImage, using images that i am downloading from a server. This works fine most of the time, but sometimes even though it will download the correct image, it will display a previously downloaded image, this seems to happen at random. I am using cocos 0.8.2. I think this is related to issue #<a href='http://www.cocos2d-iphone.org/forum/tags/349'>349</a>?.  Does anyone have a workaround for this? the issue is still open.</p>
<p>Additionally before each sprite creation, I am removing my sprite using [self removechild:mySprite], therefore as I understand this shouldn't happen at all, since I am only displaying one Sprite at a time, yet sometimes when I download the new image and use the initwithCGImage the Sprite points to a previously downloaded CGImage.</p>
<p>Here is some of my code:<br />
<pre><code>- (void) startDownload {
	self.activeDownload = [NSMutableData data];
	NSString *imageURL=[self.pList objectForKey:[NSString stringWithFormat:@&#34;%d&#34;, next]];
    // alloc+init and start an NSURLConnection; release on completion/failure
	NSLog(@&#34;Downloading from url %@&#34;, imageURL);
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                             [NSURLRequest requestWithURL:
                              [NSURL URLWithString:imageURL]] delegate:self];
    self.imageConnection = conn;
    [conn release];
}

#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.activeDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	NSLog(@&#34;ERROR DOWNLOADING&#34;);
	// Clear the activeDownload property to allow later attempts
    self.activeDownload = nil;

    // Release the connection now that it&#39;s finished
    self.imageConnection = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSLog(@&#34;FINISH DOWNLOAD&#34;);
    // Set Image and clear temporary data/image
    UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
    self.activeDownload = nil;
	if (next != [self.myArray count])
	{
		next++;
	}
	else
	{
		next= 1;
	}
	Sprite = [Sprite spriteWithCGImage:image.CGImage];
	Sprite.shearX = -.11;
	Sprite.shearY = .097;
	[Sprite setPosition:ccp(235 , 48)];
	[self addChild:Sprite z:kTag tag:2];

	//Add new Sprite to chipmunks space
	[self addSpriteToSpace];

    // Release the connection now that it&#39;s finished
    self.imageConnection = nil;

	//Release Image
	[image release];
}</code></pre>
<p>The start download is called after the removeChild happens after, which happens every so often.</p>
<p>UPDATE: This is indeed the issue because</p>
<pre><code>-(Texture2D*) addCGImage: (CGImageRef) image forKey: (NSString *)key
{
	NSAssert(image != nil, @&#34;TextureMgr: image MUST not be nill&#34;);

	Texture2D * tex;

	if( (tex=[textures objectForKey: key] ) ) {
		return tex;
	}

	tex = [[Texture2D alloc] initWithImage: [UIImage imageWithCGImage:image]];
	[textures setObject: tex forKey:key];

	return [tex autorelease];
}</code></pre>
<p>commenting the if, gets rid of this, although I know this is not a good solution.</p>
<p> Thank you.</p>
<p>-Oscar
</p></description>
		</item>

	</channel>
</rss>

