iOS7 : UIImageView Takes Forever to Appear - ios

I have a UIView which creates and inserts views using GCD. It's working on iOS7 but the UIImageViews takes forever to show.
Here's the sample code that I am using.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
for (int i = 0; i < 200; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(77 * i, 0, 77, 88)];
[scrollView addSubview:view];
dispatch_queue_t queue = dispatch_queue_create("image", NULL);
dispatch_async(queue, ^{
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo#hd.png"]];
imageView.frame = CGRectMake(0, 0, 77, 88);
[view addSubview:imageView];
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
label.text = [NSString stringWithFormat:#"Name %d", i];
[label sizeToFit];
label.center = CGPointMake(38.5, 20);
[imageView addSubview:label];
});
});
[scrollView setContentSize:CGSizeMake(view.frame.size.width * (i+1), view.frame.size.height)];
}
[self.view addSubview:scrollView];
By the way, I am creating many UIViews that's why it is threaded. Around 200 views at max.
How should I make it work? The methods inside the dispatch blocks are being called, it is just the UIViews are not being displayed.
Thanks!

Related

Centering UI elements in middle of screen programmatically

I'm trying to do something fairly simple, but just can't manage to make it happen. I have an UIImage followed by two UILabels, each on on top of the other, that I want to center in the middle of a UIView.
(empty space)
UIImage
UILabel
UILabel
(empty space)
This is what I have so far:
UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
[imageView setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[label1 setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[label2 setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[baseView addSubview:imageView];
[baseView addSubview:label1];
[baseView addSubview:label2];
[self addSubview:baseView];
But it doesn't seem to be working. All the elements are over each other. Any ideas?
Have it a try.
I add an container view to hold the imageView and the two labels, then center the container view in baseView. Set backgroundColor of container to clear color so we can't see it.
UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 175+50+50)] ;
container.backgroundColor = [UIColor clearColor] ;
[imageView setCenter:CGPointMake(container.bounds.size.width/2, imageView.bounds.size.height/2)];
[label1 setCenter:CGPointMake(imageView.center.x, imageView.bounds.size.height + label1.bounds.size.height / 2)];
[label2 setCenter:CGPointMake(imageView.center.x, label1.frame.origin.y + label1.bounds.size.height + label2.bounds.size.height / 2)];
container.center = CGPointMake(baseView.bounds.size.width/2, baseView.bounds.size.height/2) ;
[container addSubview:imageView];
[container addSubview:label1];
[container addSubview:label2];
[baseView addSubview:container] ;
[self addSubview:baseView];

Make UIScrollView Loop iOS

I apologize in advance for a basic question but I am new to iOS and Arrays but I would like to loop the UIScrollView that I made with the pictures in it once it gets to the end instead of stopping. Do I reset a variable back to 0 once it knows my pagecount = 3? How would i go about this? Thank you!
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

wait_fences: failed to receive reply: 10004003 - viewDidLoad

I'm getting this error and curious whats triggering it. From what I've read it's usually about animating inside viewDidLoad or blocking a UIAlertView.
1.If I comment out the scrollView block of code, the error goes away.
2.If I leave the scrollView code and comment out from 360Controller start to page One end. The warning goes away.
3.Is it because of memory overload?
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.hidden = YES;
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
//SCROLLVIEW Start
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[scrollView setContentSize:CGSizeMake(1024*6, 768)];
scrollView.bounces = NO;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.delegate = self;
[self.view addSubview:scrollView];
//SCROLLVIEW End
//Poster Vertical Scroller STart
posterVerticalScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(1024*3, 0, 1024, 768)];
[posterVerticalScroller setContentSize:CGSizeMake(1024, 768*3)];
posterVerticalScroller.bounces = NO;
posterVerticalScroller.pagingEnabled = YES;
[scrollView addSubview:posterVerticalScroller];
//Poster Vertical Scroller End
//Overview Start
overView = [[OverViewController alloc] initWithNibName:nil bundle:nil];
overView.view.frame = CGRectMake(1024, 0, 1024, 768);
[scrollView addSubview:overView.view];
//OverView End
//Landing start
landingView = [[LandingViewController alloc] initWithNibName:nil bundle:nil];
landingView.view.frame = CGRectMake(0, 0, 1024, 768);
[scrollView addSubview:landingView.view];
//Landing End
//360Controller Start
rotateController * rotateAnim = [[rotateController alloc] initWithNibName:nil bundle:nil];
rotateAnim.view.frame = CGRectMake(1024*2, 0, 1024, 768);
[scrollView addSubview:rotateAnim.view];
animateHouse = [[UIImageView alloc] initWithFrame:CGRectMake(100 + 1024*2, 200, 800, 450)];
// animateHouse.animationImages = rotateImg;
//animateHouse.animationDuration = 3.0;
// animateHouse.animationRepeatCount = 1;
// [animateHouse startAnimating];
animateHouse.userInteractionEnabled = YES;
animateHouse.image = [UIImage imageNamed:#"1.png"];
[self.view addSubview:animateHouse];
//360Controller End
//Poster 1 start
poster1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster01.png"]];
poster1.frame = CGRectMake(0, 0, 1250, 768);
[posterVerticalScroller addSubview:poster1];
poster1Items = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster01_items.png"]];
poster1Items.frame = CGRectMake(0, 0, 1024, 768);
[posterVerticalScroller addSubview:poster1Items];
//Poster 1 end
//Poster 2 Start
poster2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster02.png"]];
poster2.frame = CGRectMake(0, 768, 1162, 768);
[posterVerticalScroller addSubview:poster2];
poster2Items = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster02_items.png"]];
poster2Items.frame = CGRectMake(0, 768, 1024, 768);
[posterVerticalScroller addSubview:poster2Items];
//Poster 2 End
//Poster 3 Start
poster3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster03.png"]];
poster3.frame = CGRectMake(0, 768*2, 1536, 768);
[posterVerticalScroller addSubview:poster3];
poster3Items = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster03_items.png"]];
poster3Items.frame = CGRectMake(0, 768*2, 1024,768 );
[posterVerticalScroller addSubview:poster3Items];
//Poster 3 End
//PageOne Start
UIImageView * pageOneBg = [[UIImageView alloc] initWithFrame:CGRectMake(1024*4, 0, 1024, 768)];
pageOneBg.image = [UIImage imageNamed:#"page360_landing_full.png"];
[scrollView addSubview:pageOneBg];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(1024*4+750, 520, 200, 200);
[button setTitle:#"" forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadingTEst) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
//PageOne End
//Page 2
UIImageView *pageTwoBG = [[UIImageView alloc] initWithFrame:CGRectMake(1024*5, 0, 1024, 768)];
pageTwoBG.image = [UIImage imageNamed:#"Page06_Full.png"];
[scrollView addSubview:pageTwoBG];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
buttonTwo.frame = CGRectMake(1024*5+0, 550, 250, 100);
[buttonTwo setTitle:#"" forState:UIControlStateNormal];
[buttonTwo addTarget:self action:#selector(loadMap) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:buttonTwo];
//Page 2 END
}
Try putting your animation code in viewDidAppear. If the animations only need to run when the view is loaded, then use a boolean to check if the view has been reloaded.

Resources