-(void) loadView with NSMutableArray - ios

- (void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:#"11.jpg",#"22.jpg",#"33.jpg", nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
}
When I put the NSMutableArray in -(void)viewDidLoad, UIImageView displays nothing and NSLog shows NULL. Why is that?
ps. NSMutableArray worked perfectly in -(void)loadView. I've declared NSMutableArray *arrayOfImage in #interface .h file

The only way that the given could would output "NULL" in this situation is that arrayOfImages is NULL by itself.
This is only possible if arrayOfImages is declared as a weak variable.
But as #maddy pointed out, your code is all wrong: Don't call super, but assign self.view. Or use viedDidLoad instead (probably what you want here).

-(void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"11.jpg"],[UIImage imageNamed:#"22.jpg"],[UIImage imageNamed:#"33.jpg"], nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
This will work but you will only get to see 1 picture. ie the picture in the 0 index of the array.
If you want to show all then you got to apply a loop.

Related

ScrollView image addSubview issue

I'm trying to add an imageView in the ScrollView but I always get the ScrollView empty. Please where would be my issue? While I have added the delegate method <UIScrollViewDelegate> also made the connection in the storyboard.
My code:
- (void) viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void) slider {
_scrlView = [[UIScrollView alloc]init];
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
First, you don't add the scrollView to any view, so it won't be presented on screen.
Second, this:
_scrlView = [[UIScrollView alloc]init];
creates a scrollView with CGRectZero which is the same like writing:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
This means the contentSize is also CGRectZero.
Edit: If you don't want to override the creation of the scrollView (because you have already set it in Interface Builder), remove the line _scrlView = [[UIScrollView alloc]init];.
But if you are using Auto Layout in the storyboard you should call [self slider] in viewDidLayoutSubviews.
You need to set frame first, for getting its width and height,
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(YOUR_X, YOUR_X, YOUR_WIDTH, YOUR_HIEGHT)];
// do this if you have not added Scrollview in storyboard, if Added, then no need to invoke, init by line [[UIScrollView alloc] init] ;
...
...
[self.view addSubview:_scrlView];
}
I would suggest one more thing, to call your slider method from viewDidAppear method of UIView
Enjoy Coding !!
You need to allocate the ScrollView if have not used a storyboard. Also in that case, specify a frame:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
You are reallocating the imageviews, try to set image after allocating it once.
Another thing : Dont init scrollview again.
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
[_img1 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img2 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img3 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
My reply would be
if you are not using Storyboard or Interface builder , also autolayout is unchecked, set the frame of uiscrollview after its allocation and use alloc init only one time for one image. Add image on scrollview and scrollview on superview
if you are not using Storyboard or Interface builder and autolayout is checked, then put proper constraints on scrollview and imageviews. Read this
https://developer.apple.com/library/ios/technotes/tn2154/_index.html

Can't access UIImageView if created programmatically

If I create a UIImageView via Storyboard and add it as a #property into a ViewController.h, I can access that UIImageView from anywhere in the ViewController.m via [self UIImageView] or self.UIImageView.
But If I create the UIImageView programmatically from viewDidLoad as you see below, I seem to lose the ability to access it from outside viewDidLoad.
UIImageView *prevImgView = [[UIImageView alloc] init];
UIImageView *currImgView = [[UIImageView alloc] init];
UIImageView *nextImgView = [[UIImageView alloc] init];
NSArray *imageViews = [NSArray arrayWithObjects:prevImgView, currImgView, nextImgView, nil];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView];
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i=0; i<imageViews.count; i++) {
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
So later on if I want to modify the image that is being displayed in any one of the UIImageView I've created from viewDidLoad, I can't seem to access it. Does anyone know what I'm doing wrong?
You would need to create your UIImageView in your header file and then it would be available throughout the rest of the view. You will need to add it to the view in viewDidLoad though.
Header
UIImageView *image;
Main // ViewDidLoad
image = [UIImageView alloc] .....
[self.view addSubView image];
Main Function .....
[image setImage ....
The answer AgnosticDev is trying to say here is to add an ivar to your view controller's .h file... e.g.:
#implementation YHLViewController : ViewController
{
UIImageView * currImageView;
}
And then in your "viewDidLoad" method, you can allocate and initialize it via:
currImageView = [[UIImageView alloc] init];
and have access to it from anywhere else in your view controller.

Loading images into a UIScrollView

I'm hoping somebody can help me out here, I'm not sure what else to do. I have a UIScrollView that loads a set of 44 images, and then allows the user to page through them. My code was working, then suddenly it stopped. I have no idea what I have changed so any help would be much appreciated:
The previous view is a UITableView, it passes a string along with the name of the cell that was selected. Here is the ViewDidLoad Method from the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Answer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(answerPressed:)];
self.navigationItem.rightBarButtonItem = rightButton;
if([questionSetName isEqualToString:#"Limitations"]){
[self limitationsView];
}
}
Here is the limitationsView:
-(void)limitationsView{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSArray *unsortedArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"Images/Limitations"];
NSMutableArray *limitationImages = [[NSMutableArray alloc] init];
for (int x = 0; x<[unsortedArray count]; x++) {
NSString *fileName = [[unsortedArray objectAtIndex:x] lastPathComponent];
[limitationImages insertObject:fileName atIndex:x];
}
NSArray *sortedArray = [limitationImages sortedArrayUsingFunction:finderSortWithLocal
context:(__bridge void *)([NSLocale currentLocale])];
for(int i = 0; i< [sortedArray count]; i++){
CGFloat xOrigin = i * self.view.bounds.size.width;
UIImageView *limitationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];;
[limitationImageView setImage:[UIImage imageNamed:[sortedArray objectAtIndex:i]]];
limitationImageView.contentMode = UIViewContentModeScaleAspectFit;
[scroll addSubview:limitationImageView];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * [sortedArray count], self.view.frame.size.height);
[self.view addSubview:scroll];
}
When I get to the end of the LimitationsView method the sortedArray contains all 44 images, and the proper names, if I try and set the background color of the scrollview I can successfully do that. but my images are not loading and I am at a loss as to why?
I would suggest using
NSLog(#"image:%#",[UIImage imageNamed:[limitationsArray objectAtIndex:i]]);
to see if the image is null at that point. If that's the case, then there is a problem with the strings you are storing in that array i.e., they are not identical to the actual names of your images

Animating UIImageView frame by frame

I have an idea about how to add animated UIImageView using frame by frame method BUT my question is about How to animate UIImageView ALREADY added on view controller storyboard as IBOutlet UIImageView .
what will be changed at this peace of code ?!
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"bookmark1.png"],
[UIImage imageNamed:#"bookmark2.png"],
[UIImage imageNamed:#"bookmark3.png"],
[UIImage imageNamed:#"bookmark4.png"],
[UIImage imageNamed:#"bookmark5.png"],nil];
myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 131, 125)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25;
myAnimatedView.animationRepeatCount = 1;
[myAnimatedView startAnimating];
[self.navigationController.view addSubview:myAnimatedView];
I want to animate this image like that over my viewController
http://imageshack.us/a/img717/3051/bookmarkl.gif
It's even easier. First, add in .h file:
#property (nonatomic, retain) IBOutlet UIImageView *iV;
After, connect this outlet to the actual UIImageView on your storyboard. Change your code:
iV.animationImages = myImages;
iV.animationDuration = 0.25;
iV.animationRepeatCount = 1;
[iV startAnimating];
And that's all. Hope this helped
p.s. And yes, don't forget iV = nil; in - (void)viewDidUnload method
UPD: added
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.25];
After startAnimating call, and obviously added animationDone method:
- (void)animationDone {
[iV stopAnimating];
[iV setImage:[UIImage imageNamed:#"bookmark5.png"]];
}
Well it will be pretty much the same. You dont need to allocate your image view [[UIImageView alloc] init] nor do you need to add it to the viewController. The rest is the same.

UIView transitionFromView

I'm pretty new on iOs programming. And I'm stuck at a (i'm sure) very simple issue.
Don't know what i'm doing wrong...
-My viewDidLoad:
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 768, 1024);
UIView *uno=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainView.image = [UIImage imageNamed:#"photo.jpg"];
[uno addSubview:mainView];
UIView *dos=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainViewDos = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainViewDos.image = [UIImage imageNamed:#"Default.png"];
[dos addSubview:mainViewDos];
//
[self.view addSubview:uno];
//
[self anima:uno:dos];
And my anima method:
-(void) anima:(UIView *)uno:(UIView *)dos{
[UIView transitionFromView:uno
toView:dos
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
It changes the view but without transition...
Thanks
You can't perform an animation within your viewDidLoad--the view changes you make in there are all executed before the view is actually displayed, which is what you're seeing.
Are you trying to show this animation when the view is first displayed? If so, you can get it to work by putting the animation on a timer. Note that with this approach, you'll also have to refactor your anima method a bit to take a single argument.
In your viewDidLoad:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:uno, #"uno", dos, #"dos", nil];
[self performSelector:#selector(anima) withObject:views afterDelay:0.1];
Then change your anima method to:
-(void) anima:(NSDictionary *)views {
[UIView transitionFromView:[views objectForKey:#"uno"]
toView:[views objectForKey:#"dos"]
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}

Resources