UIImageView doesn't update
I have a problem with my UIImageView that doesn't update. So, it's like this: I have a UIScrollView that contains an UIImageView (called imageView). Now, imageView , should contain more UIImageViews. Those UIImageViews I add from code but they do not appear.
This is the code:
for(i = 0 ; i < NrOfTilesPerHeight ; i++)
for(j = 0 ; j < NrOfTilesPerWidth ; j++)
{
imageRect = CGRectMake(j*TILE_WIDTH,i*TILE_HEIGHT,TILE_WIDTH, TILE_HEIGHT);
image = CGImageCreateWithImageInRect(aux.CGImage, imageRect);
if(!data[i][j])
NSLog(@"data[%d][%d] is nil",i,j);
context = CGBitmapContextCreate (data[i][j], TILE_WIDTH, TILE_HEIGHT,
bitsPerComponent, bitmapBytesPerRow, colorSpace,
开发者_JAVA技巧 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
if (context == NULL)
{
free (data);
printf ("Context not created!");
CGColorSpaceRelease( colorSpace );
}
CGContextDrawImage(context, CGRectMake(0, 0, TILE_WIDTH, TILE_HEIGHT), image);
data[i][j] = CGBitmapContextGetData (context);
memcpy(originalData[i][j],data[i][j],TILE_WIDTH*TILE_HEIGHT*numberOfCompponents);
CGContextFlush(context);
CGImageRelease(image);
UIImageView *imgView = [[UIImageView alloc] init];
[imgView setTag:i*10+j];
CGRect frame = imgView.frame;
frame.origin.x = j * (TILE_WIDTH+5) * initialScale;
frame.origin.y = i * (TILE_HEIGHT+5) * initialScale;
frame.size.width *= initialScale;
frame.size.height *= initialScale;
[imgView setFrame:frame];
[imageView addSubview:imgView];
[self updateTileAtLine:i andRow:j];
[imgView release];
CGDataProviderRelease(dataProvider);
CGImageRelease(cgImage);
}
- (void) updateTileAtLine: (int) i andRow: (int) j
{
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, data[i][j], bitmapByteCount, NULL);
CGImageRef cgImage = CGImageCreate(TILE_WIDTH, TILE_HEIGHT, bitsPerComponent,
bitsPerPixel, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, dataProvider, NULL, false, kCGRenderingIntentDefault);
UIImage *myImg = [UIImage imageWithCGImage:cgImage];
UIImageView *auxImageView = (UIImageView*) [imageView viewWithTag:(i*10+j)];
[auxImageView setImage:myImg];
CGDataProviderRelease(dataProvider);
CGImageRelease(cgImage);
}
Now...this doesn't crashes...so everything is non-nil and ok. If instead of using viewWithTag , I alloc init a new UIImageView and add it to imageView, it will appear. But I don't want to do another copy of the view since this updateTile method will be called quite often.
My question is: Why doesn't the auxImageView appear? It very much should.
Thank you.
Regards, George
Try this
for(UIView *view in [imageView subviews]) {
if(view.tag == i*10+j) {
UIImageView *auxImageView = (UIImageView*) view;
[auxImageView setImage:myImg];
}
}
精彩评论