0

I have seen many of the posts on this site as well as many more on Google, and I am stuck on what I am sure is something easy. I am new to iOS, and I have most likely been reading the other posts incorrectly. I am trying to learn and write my own code with my own logic and attempts, so mine looks a good deal different than the others I have found... which is why it doesn't work I am sure!

I am trying to get a picture show using swipe from right to left, but I want the next picture to be pulled over and slide into place, instead of the instant switch I have done here. Based on many recommendations, I am trying to use a UIScrollView. I can't get anything to load and the app crashes on the last line of loadImages. I have debugged but can't seem to figure out why it is not showing the subView.

Here is my viewDidLoad:

    [super viewDidLoad];
// Do any additional setup after loading the view.
i = 0;
//add UIPanGestureRecognizer
UIPanGestureRecognizer *aPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(loadImages)];
[aPan setMaximumNumberOfTouches:1];
[aPan setMinimumNumberOfTouches:1];
[self.view addGestureRecognizer:aPan];


_PhotoBundle = [[NSBundle mainBundle] pathsForResourcesOfType:@".jpg"inDirectory:@"Otter_Images"];

_PhotoArray = [[NSMutableArray alloc] initWithCapacity:_PhotoBundle.count];
for (NSString* path in _PhotoBundle)
{
    [_PhotoArray addObject:[UIImage imageWithContentsOfFile:path]];
}

[self loadImages];

Here is my loadImages:

-(void)loadImages

{ NSLog(@"in loadImages");

_currentImage = [_PhotoArray objectAtIndex:i];
_nextImage = [_PhotoArray objectAtIndex:i + 1];
_prevImage = [_PhotoArray objectAtIndex:i -1];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[scrollView setScrollEnabled:YES];
[scrollView setClipsToBounds:YES];

if (i <= _PhotoArray.count)
{
    [scrollView addSubview:_currentImage]; ////crash!!!!
}
else
{
    _currentImage = [_PhotoArray objectAtIndex:0];
    [scrollView addSubview:_currentImage]; /// Crash!!!!
}

NSLog(@"end of loadImages");

}

I have left off functionality until I can get one image loaded. I will then add the functionality for the slide.... I hope I am approaching this right.

Thank you very much for any help!!

edit:

Here is my interface in my .m that sets up the ImageViews:

@interface ScrollViewViewController ()

@property (nonatomic, retain) NSArray *PhotoBundle;
@property (nonatomic, retain) NSMutableArray *PhotoArray;
@property (nonatomic, retain) UIImageView *currentImage;
@property (nonatomic, retain) UIImageView *nextImage;
@property (nonatomic, retain) UIImageView *prevImage;

@end
3
  • If your question involves a crash, please include the crash log, it is a great help. Commented Oct 26, 2012 at 21:24
  • [scrollView addSubview:_currentImage]; ////crash!!!! - This will crash because you are supposed to add an imageview in scrollview and set its imageview.image = _currentImage. You cant directly add it like this. Commented Oct 26, 2012 at 23:41
  • Thanks, I can't quite figure out what you mean? Set the scroll view to the image? or set the subview to the image? Commented Oct 27, 2012 at 4:17

1 Answer 1

1

You are adding UIImage objects as subviews. You can't do this. You have to make a UIImageView first - only UIView subclasses can be added as subviews.

Making an image view is simple - you can alloc/initWithImage: and pass in your image object. You'll need to adjust the frames of each image view otherwise they'll all be on top of each other.

Additionally, you don't need a pan gesture recogniser if you're using a scroll view. The scroll view handles this for you.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank for the response. my _currentView object is an UIImageView setup in the @interface. Can you not add a UIImage to a UIImageView? do I need to create a new UIIMageView each time?
There is no _currentView that I can see in your code. You can assign a new image to an existing image view, yes, just set the .image property.
I had to leave town, sorry for the delay. I just edited my question to include my interface where I setup my _currentImage.... I realize I said _currentView in my last post and I am sorry for the confusion. I am trying your suggestions now.
so when I add the .image to the _currentImage UIImageView, and add that as a subview to the scrollView it does not crash on that line. I get a white screen which I imagine has to do with frames.
Probably, yes. Check the value of self.currentImage in the debugger, it may be nil (I can't see where you set it up) or have an odd frame.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.