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!
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;
}
I have a UITable and when I click on a row I load a set of images.
I create the images with
UIImage *image = [UIImage imageWithData:data];
The problem is that ever time I click on the same row, the images are re-allocated into memory, like if no cache it is used and no memory is freed, so the allocated memory keep increasing. (I see this using the Inspector on both the device and the emulator)
I am releasing everything and also, with clang, everything looks fine.
The point is that after a while my app crashes.
Any suggestion or idea?
EDIT:
Here is the portion of code I think is generating the issue:
UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];
for (int i=0; i<[contentArray count]; i++) {
float targetWidth = rectScrollView.size.width;
float targetHeight = rectScrollView.size.height;
Link *tmp_link = [contentArray objectAtIndex:i];
AsyncImageView* asyncImage = nil;
if(tmp_link!=nil){
CGRect imageFrame = CGRectMake(targetWidth*i, 0, targetWidth, targetHeight);
//asyncImage = [[[AsyncImageView alloc] initWithFrame:imageFrame ] autorelease];
asyncImage = [[AsyncImageView alloc] initWithFrame:imageFrame ];
asyncImage.contentMode = UIViewContentModeScaleAspectFit;
NSURL* url = [NSURL URLWithString:[[IMG_SERVER_URL stringByAppendingString: tmp_link.url] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
[asyncImage loadImageFromURL:url];
[self.scrollView addSubview:(UIImageView *)asyncImage];
[asyncImage release];
}
if ([tmp_link.caption length] > 0 && asyncImage!=nil) {
float marginTop = 25;
UILabel *caption = [[UILabel alloc] init];
caption.text = tmp_link.caption;
caption.textAlignment = UITextAlignmentCenter;
caption.textColor = [UIColor whiteColor];
caption.font = [UIFont systemFontOfSize:12];
caption.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.4];
CGRect captionFrame = CGRectMake((i * targetWidth), marginTop+10, targetWidth, 20.0f);
caption.frame = captionFrame;
[self.scrollView addSubview:caption];
[caption release];
}
//[asyncImage release];
}
[main addSubview:scrollView];
[scrollView release];
if (pageControlEnabledTop) {
rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
}
else if (pageControlEnabledBottom) {
//rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
rectPageControl = CGRectMake(0, (scrollHeight), scrollWidth, 15);
}
if (pageControlEnabledTop || pageControlEnabledBottom) {
//pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
pageControl = [[UIPageControl alloc] initWithFrame:rectPageControl];
pageControl.numberOfPages = [contentArray count];
pageControl.currentPage = page;
[main addSubview:pageControl];
[pageControl release];
}
if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
//if (margin) [margin release];
return (UIScrollView *)main;
And it is used like this:
reviewImagesImage = [[IGUIScrollViewImage alloc] init];
if([currentReview.linkScreenshots count]>0){
//reviewImages
reviewImages = [[UIView alloc] init];
//placing the images
CGRect frameImages = reviewImages.frame;
frameImages.origin.x = 10;
frameImages.origin.y = (infoView.frame.origin.y + infoView.frame.size.height + 10.0f);
frameImages.size.width = 300;
frameImages.size.height = 350.0f;
reviewImages.frame = frameImages;
reviewImages.backgroundColor = [UIColor clearColor];
[reviewImagesImage setContentArray:currentReview.linkScreenshots];
[reviewImagesImage setWidth:reviewImages.frame.size.width andHeight: reviewImages.frame.size.height];
[reviewImagesImage enablePageControlOnBottom];
[reviewImagesImage setBackGroudColor: [UIColor clearColor]];
UIScrollView *tmp_sv = [reviewImagesImage getWithPosition:0];
[reviewImages addSubview:tmp_sv];
[self.view addSubview:reviewImages];
[reviewImages release];
}
I have noticed that if I try to release reviewImagesImage my app crashes :/
Of course no cache is used because new UIImage will be created from the data every time. If memory usage significantly increased like you have said, it is most likely that it is our fault that unintentionally keep the reference retained somewhere.
Once you've set your image with imageWithData, save the image to the filepath in the app bundle.
Hi I have populated uiscrollview with images (code is bellow) but I don't know how to add some event to images inside. I want to double tap on image inside scrollview and get some event. Any direction how to achieve this?
arrayOfImages = [[NSMutableArray alloc] init];
NSString *img;
for (img in imgArray)
{
[arrayOfImages addObject:[UIImage imageNamed:img]];
}
NSLog(#"Array initialization complete...");
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 29);
[self.view addSubview:scrollView];
NSLog(#"Scroll View initialization setup complete...");
UIImage *imageToAdd;
int x = 0;
int y = 0;
for (imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 29, 29);
x += 29;
[scrollView addSubview:temp];
}
NSLog(#"Adding images to outlet complete...");
Add a UITapGestureRecognizer to the UIImageView(s).
Look at this guide for more info.
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];
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];
}