I'm having trouble deallocating a UISCrollView that contains images.
There's something wrong with my code, but I cannot understand where.
this is a snippet of my code. It basically create a loops adding a uiscrollview with an image and removing.
If you run the code with instruments, no memory will be freed.
I also add some check for the retainCount, with no luck at all..
here's the code
- (void)loadView {
[super loadView];
CGRect theRect = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:theRect];
[view setBackgroundColor:[UIColor purpleColor] ];
self.view = view;
[view release];
UIView *pippo = [[UIView alloc] initWithFrame:theRect];
[pippo setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pippo];
[pippo release];
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
int dd = [img retainCount];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
dd = [v2 retainCount];
[v2 release];
dd = [v2 retainCount];
dd = [img retainCount];
[self.view addSubview:scroll];
[img release];
dd = [img retainCount];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
-(void)scrollRemove:(id)o {
int dd = [scroll retainCount];
UIImageView *theV = [[scroll subviews] objectAtIndex:0];
dd = [theV retainCount];
[scroll removeFromSuperview];
dd = [theV retainCount];
[theV release];
dd = [theV retainCount];
dd = [scroll retainCount];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
The issue is that you are releasing img when you shouldn't (which is probably being masked by the view hierarchy never being released), and you aren't releasing scroll when you should.
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
int dd = [img retainCount];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
[img release];
dd = [img retainCount];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
This should be:
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
UIImageView *v2 = [[UIImageView alloc] initWithFrame:theRect];
[v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
Of course if you do this you need to also make a slight change in your view removal path:
-(void)scrollRemove:(id)o {
[scroll removeFromSuperview];
[scroll release];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
I've basically reached the same conclusions as #Louis, but also made some comments within the code as to what I removed and why.
- (void)loadView {
[super loadView];
CGRect theRect = CGRectMake(0, 0, 320, 480);
UIView *view = [[UIView alloc] initWithFrame:theRect];
[view setBackgroundColor:[UIColor purpleColor] ];
self.view = view;
[view release];
UIView *pippo = [[UIView alloc] initWithFrame:theRect];
[pippo setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pippo];
[pippo release];
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
-(void)scrollAdd:(id)o {
CGRect theRect = CGRectMake(0, 0, 320, 480);
int numero = 1;
scroll = [[UIScrollView alloc] initWithFrame:theRect];
[scroll setContentSize:CGSizeMake( 320*numero,1)];
[scroll setScrollEnabled:YES];
// img is already autoreleased, you're releasing again down below
// --- UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"koala1b" ofType:#"jpg"]];
UIImageView *v2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"koala1b.jpg"]];
v2.frame = theRect;
//--- [v2 setImage:img];
[scroll addSubview:v2];
[v2 release];
[self.view addSubview:scroll];
// NO !; img is autoreleased from imageWithContentsOfFile
//--- [img release];
[self performSelector:#selector(scrollRemove:) withObject:nil afterDelay:2.0f];
}
-(void)scrollRemove:(id)o {
//--- UIImageView *theV = [[scroll subviews] objectAtIndex:0];
[scroll removeFromSuperview];
// you don't own theV because you didn't create it here, or send it a retain. So NO release
// it also appears that you think you're responsible for releasing or cleaning up
// a view's subviews. NO. Just call [scroll removeFromSuperview] and let scroll view
// clean it's own resources.
//--- [theV release];
[scroll release];
scroll = nil;
[self performSelector:#selector(scrollAdd:) withObject:nil afterDelay:2.0f];
}
Related
I have an application in which a pickerview is ther with images.I had done that with this.`
picker1=[[UIPickerView alloc] init];
picker1.frame= CGRectMake(30,90,155,316);
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker1.tag = 1;
[self.view addsubview:picker1];
and i am using viewForRow delegate methode to load the images.i need to increase the images frame there inside the pickerview.`
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",[self.array objectAtIndex:row]]];
UIImageView *icon = [[UIImageView alloc] initWithImage:img];
//temp.frame = CGRectMake(170, 0, 30, 30);
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectMake(155,75,95,35)] autorelease];
[tmpView insertSubview:icon atIndex:0];
picker1.backgroundColor = [UIColor clearColor];
[(UIView*)[[picker1 subviews] objectAtIndex:2] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:0] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:1] setAlpha:0.0f];
[(UIView*)[[picker1 subviews] objectAtIndex:3] setAlpha:0.0f];
[tmpView setUserInteractionEnabled:NO];
[tmpView setBackgroundColor:[UIColor clearColor]];
[tmpView setTag:row];
return tmpView;
but when i try to increase the frame of the view its position only changing,not the size.can anybody help me to achieve this?
implement the following pickerview delegate method..
- (CGSize)rowSizeForComponent:(NSInteger)component{
CGSize size = CGSizeMake(320, 60); //input ur custom size
return size;
}
In this code I want to do changes like as instead of button I want to create images in image view with scroll view with zoom in and zoom out event on image. And two or three button on every image view so that I can implement some event on button. How do that?
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=460;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(#"Current TAG: %i", whichButton);
if(whichButton==1)
{
first=[[FirstImage alloc]init];
[self.navigationController pushViewController:first animated:YES];
}
}
You need Image instead of Button right? Then Try This:
scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scroll.backgroundColor=[UIColor clearColor];
scroll.pagingEnabled=YES;
scroll.contentSize = CGSizeMake(320*33, 300);
CGFloat x=0;
for(int i=1;i<34;i++)
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]]];
[scroll addSubview:image];
[image release];
x+=320;
}
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator=NO;
[scroll release];
I am write this code but the warning is show "insertSubview" may not respond
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"GO-21-TabBarColorx49.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[[self tabBar] addSubview:v];
[v release];
}
Did you try
[[self tabBar] insertSubview:v atIndex:0];
Instead of addSubview:
Source: Changing Tint / Background color of UITabBar
I use the following to display scrollview and pagecontrol
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 179)];
pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 179, 320, 20)];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
int index=[[self._parameters objectForKey:#"index"] intValue];
NSString *imageNamePrefix=[[activeAppIdList objectAtIndex:index] objectForKey:#"AppID"];
int noOfScreenShot=[[[activeAppIdList objectAtIndex:index] objectForKey:#"NoOfScreenShot"] intValue];
CGFloat cx = 0;
for (int j=1;j<=noOfScreenShot ; j++) {
UIImage *image =[[UIImage alloc] init];
image= [self loadImage:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
if (image == nil) {
NSString *urlString = [[NSString alloc]initWithFormat:#"http://localhost/MoreApp/images/%#_%d.jpg",imageNamePrefix,j];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *temp=[[UIImage alloc]initWithData:imageData] ;
[self saveImage:temp withName:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
image = temp;
[temp release];
[urlString release];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[image release];
imageView.frame = CGRectMake(cx,0,scrollView.frame.size.width,scrollView.frame.size.height);
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
NSLog(#"No Of pages..%d",noOfScreenShot);
pageControl.numberOfPages = noOfScreenShot;
pageControl.currentPage = 0;
scrollView.contentSize = CGSizeMake(cx,scrollView.frame.size.height);
NSLog(#"Width:%f",scrollView.frame.size.width);
[ refToSelf.instructionView addSubview:scrollView];
[ refToSelf.instructionView addSubview:pageControl];
[scrollView release];
[pageControl release];
scrollview is ok but pagecontroll didn't showed...
can help??
If the page control's and container's background color is the same (by default white) page control won't be visible.
For anyone who finds this, another silly mistake you can make is to not set the numberOfPages attribute on the UIPageControl, which will also cause it to not display.
My mistake was equally annoying - developing with storyboard in 3.5 inch view, and built on 4 inch device on simulator so it was offscreen. Silly!
Help me.
addSubview doesn't work.
I want to add "ContentView" on scrollView.
But "ContentView" doesn't appear on screen.
"ContentView" is UIView.
When I change to self.view=scrollView from [self.view addSubview:scrollView],
addSubview doesn't work.
Please teach me how to add UIView on this screen!!
- (void)viewDidLoad {
[super viewDidLoad];
scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init];
mainStatic = [[MainStatics alloc] init];
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Files" ofType:#"plist"];
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];
kNumImages = [dataFiles count];
CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];
NSUInteger i;
for (i=0;i<[dataFiles count];i++)
{
UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = NO;
CGRect rect = imageView.frame;
rect.size.height = 480;
rect.size.width = 320;
imageView.frame = rect;
imageView.tag = i+1;
[imageViewArray addObject:imageView];
}
self.view = scrollView;
[self setPagingMode];
UIView* contentView;
CGRect scRect = CGRectMake(0, 436, 320, 44);
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];
[self addSubview:contentView];
}
I used image array in following function.
toolView is UIView with UIToolbar.
So I want to add toolbar to screen.
Yes, touchClass is subclass of UIScrollView.
1.I see.I forgot release this.Thank you.
2.OK. I modified this.But "ContentView" didn't apear.
are there another reason?
- (void)setPagingMode {
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imageViewArray){
[scrollView addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);
scrollViewMode = ScrollViewModePaging;
}
Source isn't clear.
You never use array with UIImageView. There is no info what is toolView etc...
Please provide more detailed source. Is touchClass a subclass of UIScrollView?
What I noticed in your code:
You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];