Can anyone help me out with explaining why my array is crashing.
Basically I have two buttons which change the image in the imageView.
h:
#interface CaseStudySecondPageViewController : UIViewController
{
//scroller and back and forward buttons for custom control.
__weak IBOutlet UIScrollView *scroller;
__weak IBOutlet UIButton *back;
__weak IBOutlet UIButton *forward;
//app delegate to return the selected case study from the other controller.
AppDelegate *del;
//variables for displaying the case studies
NSArray *myImageArray;
NSInteger localSelctorInt;
}
//setup a IBOutlet to allow the image to be changed.
#property (weak, nonatomic) IBOutlet UIImageView *logoImage;
#end
m:
update method:
-(void)Updater
{
[logoImage setImage:[myImageArray objectAtIndex:localSelctorInt]];
}
previous and next buttons:
//returns to previous image if back button is clicked.
-(void) back:(id)sender
{
if (localSelctorInt > 0)
{
localSelctorInt--;
}
else
{
localSelctorInt = 0;
}
[self Updater];
}
//returns to next image if forward button is clicked. increase 7 if array size changes.
//1 removed from int as in starts at 1 and array starts at 0.
-(void) forward:(id)sender
{
if (localSelctorInt < 7)
{
localSelctorInt++;
}
else
{
localSelctorInt = 7;
}
[self Updater];
}
and finally my image array is declared in:
- (void)viewDidLoad
{
[super viewDidLoad];
//setup the delegate to allow access to the int which was modified on the page before.
del = [[UIApplication sharedApplication] delegate];
//assign a local variable to the int from the previous page.
localSelctorInt = *(del.selectorInt);
//create back and forward buttons as UIButtons first.
[back addTarget:self action:#selector(back: ) forControlEvents:UIControlEventTouchUpInside];
[forward addTarget:self action:#selector(forward:) forControlEvents:UIControlEventTouchUpInside];
//pass the ui buttons into ui bar items.
UIBarButtonItem *UIBack = [[UIBarButtonItem alloc] initWithCustomView:back];
UIBarButtonItem *UIForward = [[UIBarButtonItem alloc] initWithCustomView:forward];
//add these to an array (notice item"s").
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:UIForward, UIBack, nil];
//initialize the array with all of the images for the case studies logos.
myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Logo_Arm.png"],
[UIImage imageNamed:#"Logo_Fife"],
[UIImage imageNamed:#"Logo_Findel.png"],
[UIImage imageNamed:#"Logo_BirkBeck.png"],
[UIImage imageNamed:#"Logo_NHS_Dudley.png"],
[UIImage imageNamed:#"Logo_NHS_Kensignton.png"],
[UIImage imageNamed:#"Logo_Yorkshire_Water.png"],
[UIImage imageNamed:#"Logo_Uni_Hertfordshire.png"],
nil];
//call update method once to load the first image selected from the previous page.
[self Updater];
}
now my app crashes when it trys to index the 4th image (3rd in the array):
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 ..
2]'
It's probably something really simple, but id appreciate your help, cheers.
If you look at your exception, it says that your array contains 3 objects only ( ... bounds [0..2] ... ). So, my guess is that your fourth image doesn't exist ... Check spelling of [UIImage imageNamed:#"Logo_BirkBeck.png"]. Does this image really exist? It returns nil, list of objects is nil terminated, so, your array does contain 3 images only instead of 8 images.
Side Note: Don't use magic constants like localSelctorInt < 7. Always rely on real number of elements in array (_myImageArray.count). It's a way to hell ...
2nd Side Note: Don't declare ivars without underscore prefix. Do use something like this _myImageArray for ivar name.
I guess its due to not allocating the array
myImageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"Logo_Arm.png"],
[UIImage imageNamed:#"Logo_Fife.png"],
[UIImage imageNamed:#"Logo_Findel.png"],
[UIImage imageNamed:#"Logo_BirkBeck.png"],
[UIImage imageNamed:#"Logo_NHS_Dudley.png"],
[UIImage imageNamed:#"Logo_NHS_Kensignton.png"],
[UIImage imageNamed:#"Logo_Yorkshire_Water.png"],
[UIImage imageNamed:#"Logo_Uni_Hertfordshire.png"],
nil];
Related
My main aim is to have one background for all of my ViewControllers. Every ViewController I have has a clear background.
To do this, I have made one UIViewController (called backgroundViewController) that will act as the subview to all my other ViewControllers. It has one UIImageView which displays this particular background. I will then add this backgroundViewController as a subview of all my other ViewControllers.
The problem is - this imageView won't show as a subview!
This is how I display the imageView:
- (void)viewDidLoad
{
if ([musicPlayer playbackState] == MPMusicPlaybackStateStopped) {
UIImage *artworkBackgroundView;
artworkBackgroundView = [UIImage imageNamed:#"noArtworkBackground"];
UIImage *effectImage = nil;
backgroundView.image = effectImage;
[backgroundView setImage:artworkBackgroundView];
backgroundView.image = effectImage;
}
}
- (void) handle_NowPlayingItemChanged: (id) notification
{
if ([musicPlayer playbackState] != MPMusicPlaybackStateStopped) {
// Get artwork for current now playing item
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
UIImage *artworkBackgroundView = [artwork imageWithSize: CGSizeMake(618, 618)];
if (!artworkImage) {
artworkBackgroundView = [UIImage imageNamed:#"noArtworkBackground"];
artworkImage = [UIImage imageNamed:#"noArtwork"];
}
[backgroundView setImage:artworkBackgroundView];
}
}
As you can see, backgroundView changes each time the music player skips song.
To test that backgroundViewController does show as a subview, I added another imageView and changed its image to a static .png in Interface Builder, and that shows correctly as a subview.
This is how I make it a subview for other ViewControllers:
backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:#"backgroundViewController" bundle:nil];
[self.view insertSubview:backgroundVC.view atIndex:0];
What I want is the UIImageView called backgroundView to show up when it is being called as subview.
I have tested that backgroundView does change according to what song is playing, and it works correctly.
What am I doing wrong? backgroundView refuses to show up as a subview?! I've searched a ton about adding ViewControllers as subviews but I can't find a similar problem.
Any help would be much appreciated, thanks! :)
This worked for me:
backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:#"backgroundViewController" bundle:nil];
[self addChildViewController:backgroundVC];
[self.view addSubview:backgroundVC.view];
I found the solution here. I need to read more about 'Custom Containers'.
I hope this makes sense...I have an NSMutableArray that I'm attempting to store multiple UIScrollView's in. Each UIScrollView is going to have multiple images and the ultimate goal is to be able to allow the user to swipe vertically for categories (each instance of UIScrollView) and horizontally for each image in that category (each instance of UIImageView). For now, until I get this code to work, I'm just creating 1 UIScrollView with 1 image and I'm adding that to scrollViewManager. This seems to add everything correctly, but when I leave this function, game over. My Array is empty. I don't understand. Am I supposed to do some sort of deep copy when I add to the Array so it doesn't get destroyed. Perhaps I'll figure it out when I wake up tomorrow, but for now, I'd like to break everything in sight. Thanks, in advance!
EDIT: Sorry for lack of details, I posted this rather late. My project is using ARC, so I'm not releasing scrollView anywhere. Here is the declaration for scrollViewManager in my header file:
#property (nonatomic, retain) NSMutableArray *scrollViewManager;
Here is the code I use to retrieve my scrollView:
UIScrollView *test = (UIScrollView*) [self.scrollViewManager objectAtIndex: 0];
And here is the code to initialize the scrollView and array:
scrollViewManager = [[NSMutableArray alloc] initWithCapacity: 1];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSString *imageName = #"pic1.png";//[NSString stringWithFormat:#"pic%d.jpg", i+1];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = 400;
rect.size.width = 300;
imageView.frame = rect;
imageView.tag = 1; // tag our images for later use when we place them in serial fashion
[scrollView addSubview:imageView];
[self.scrollViewManager addObject: scrollView];
There are some things that are kind of vague. For example:
Are you using ARC, if not and you are releasing your scrollView anywhere?
Are you using the auto-create-synthesize thing?
What type of property is your scrollViewManager (weak, strong, etc).
If you are creating your ivars automatically when defining a property, your ivar should be called _scrollViewManager instead of scrollViewManager.
The use of self.scrollViewManager is correct when using this method.
I always do this kind of stuff the other way around. When I have a property like this:
#property (nonatomic, strong) NSMutableArray* myArray;
I always initialize them using the self keyword:
self.myArray = [NSMutableArray new];
And when modifying it, I always use the generated ivar:
[_myArray addObject:myFineObject];
I think this is something I kept using when moving from non-ARC to ARC. Not sure if it still the way to go, but it works like expected.
This is what I did:
#implementation BGUIActivityIndicator
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self customInitialize];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
[self customInitialize];
return self;
}
-(void)customInitialize
{
UIView * theImageView= [self findASubViewforClass:[UIImageView class]];//
UIImageView * theImageView1 = (UIImageView *) theImageView;
theImageView1.image = [UIImage imageNamed:#"spinner_blue"];
[theImageView1.image saveScreenshot];
while (false) ;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Everything seems perfect. [theImageView1.image saveScreenshot]; jot down both the old and new view perfectly.
However, nothing changes. Why?
I am not exactly asking how to change the image of UIActivityIndicator. There are tons of it already. I want to use it by subclassing UIActivityIndicator because I think it's the most elegant solution. I can't seem to do that.
In particular, I am asking why my approach, which works for changing background of search controller, for example, doesn't work for this.
According to the UIActivityIndicatorView Class Reference ,there is no way/ chance to change the image through sub-classing.
However you can change its activityIndicatorViewStyle , color of the activity indicator,UIActivityIndicatorStyle etc..
I think, without sub-classing, the class class UIImageView provides a very useful and simple way to implement such a thing. The only thing you have to do is to:
1.Provide a number of images that reflect your indicator animation.
2.Create a new UIImageView instance and set images and animation duration.
3.Position your custom activity indicator within your current view.
SAMPLE CODE:
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"status1.png"];
UIImageView *activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"status1.png"],
[UIImage imageNamed:#"status2.png"],
[UIImage imageNamed:#"status3.png"],
[UIImage imageNamed:#"status4.png"],
[UIImage imageNamed:#"status5.png"],
[UIImage imageNamed:#"status6.png"],
[UIImage imageNamed:#"status7.png"],
[UIImage imageNamed:#"status8.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.8;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/2,
self.view.frame.size.height/2
-statusImage.size.height/2,
statusImage.size.width,
statusImage.size.height);
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self.view addSubview:activityImageView];
See the full details Here
I've got an app where multiple UIImages can be added to the view. Those images can then be dragged around the screen. How can I check which image has been dragged and then save that image's coordinates to a file and no other UIImage in the same view. I need a way of tagging each UIImage if possible to separate them out and identify them each individually. Hopefully this makes sense!
This is how I'm adding each UIImage to the view:
CGRect imageFrame = CGRectMake(activeView.center.x - 50, activeView.center.y - 50, 200, 200);
imageResizableView = [[SPUserResizableView alloc] initWithFrame:imageFrame];
UIImage *image = [UIImage imageNamed:#"galaxy.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageResizableView.contentView = imageView;
imageResizableView.delegate = self;
[activeView addSubview:imageResizableView];
You can use the tag property on UIVIew.
As per mentioned in the official doc:
tag An integer that you can use to identify view objects in your application.
#property(nonatomic) NSInteger tag Discussion The default value is 0. You can set the value of this tag and use that value to identify the view later.
UIImageViews are UIView subclasses, so have a tag property. Set that for each image, then when you want that one image [self.subViews viewWithTag:number]; to find it.
EDIT: If I understand this, there are many UIImageViews, but multiple imageViews may show the same image.
Assuming that, then you partition the tag's 32 bits to say 16 bits upper as the unique imageView number, and the lower 16 are the image number. You will then need to iterate through all subviews looking for the image. You can use macros to make this easier:
#define TAG_NUMBER(imageViewNum, imageNum) ((imageViewNum << 16) | imageNum)
#define IMAGE_FROM_TAG(tag) (tag & 0xFFFF)
etc
when you want to find all imageviews showing that image:
for(UIVIew *view in self.subviews) {
if(![view isKindOf:[UIIMageView class]]) continue;
int imageNum = IMAGE_FROM(view.tag);
if(imageNum == theOneIwant) {
save frame of "view"
}
}
If integer tags are good enough, then use the tag property which already exists for UIViews.
However, if you want something more, you can use obj_setAssociatedObject to add a tagName property to UIView.
#interface UIView (tagName)
#property (nonatomic, copy) NSString *tagName;
#end
--
#import <objc/runtime.h>
#implementation UIView (tagName)
static char tagNameKey[1];
- (NSString*)tagName {
return objc_getAssociatedObject(self, tagNameKey);
}
- (void)setTagName:(NSString *)tagName {
objc_setAssociatedObject(self, tagNameKey, tagName, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#end
which can then be used like so...
NSString *imageName = #"galaxy.jpg";
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tagName = imageName;
and later...
NSString *imageName = imageView.tagName;
EDIT
Of course, you can add whatever you want, for example to mimic viewWithTag
- (UIView*)viewWithTagName:(NSString *)tagName {
if ([self.tagName isEqualToString:tagName]) {
return self;
}
UIView *result = nil;
for (UIView *view in self.subviews) {
if ((result = [view viewWithTagName:tagName]) != nil) {
return result;
}
}
return nil;
}
in my nib file I have a UIImageView that I have linked to my variable IBOutlet UIImageView* imgH6; I need to play several animations therefore I have decided to play them on imgH6. for example if I want to start animating a group of images I will do: [self playAnimation1];
-(void) playAnimation1{
NSArray *imagesArrayDilema;
imagesArrayDilema = [NSArray arrayWithObjects:
[UIImage imageNamed:#"dilema1-02.png"],
[UIImage imageNamed:#"dilema1-03.png"],
[UIImage imageNamed:#"dilema1-04.png"],
[UIImage imageNamed:#"dilema1-05.png"]
// .....
// there are several images
, nil];
imgH6.animationImages = imagesArrayDilema;
// I will then start the animation as:
wait = ((double)[imgH6.animationImages count])/30;
[imgH6 setAnimationRepeatCount:1];
imgH6.animationDuration = wait;
[imgH6 startAnimating];
}
-(void) playAnimation2{
NSArray *imagesArrayDilema;
imagesArrayDilema = [NSArray arrayWithObjects:
[UIImage imageNamed:#"differentImage-01.png"],
[UIImage imageNamed:#"differentImage-02.png"],
[UIImage imageNamed:#"differentImage-03.png"],
[UIImage imageNamed:#"differentImage-04.png"],
// .....
// there are several images
, nil];
imgH6.animationImages = imagesArrayDilema;
// I will then start the animation as:
wait = ((double)[imgH6.animationImages count])/30;
[imgH6 setAnimationRepeatCount:1];
imgH6.animationDuration = wait;
[imgH6 startAnimating];
}
///.....
// ...
-(void) playAnimation8{ // etc...
so when I call [self playAnimation1]; there is no problem. the animation animates fine. I may also call [self playAnimation7]; and a different group of images animate. I am able to do this several times but eventually the application crashes! In order to fix this I have done the following: I have a global BOOL variable called isAnimating and what that does is that every time there is an animation in place you will not be able to animate a different group of images until the animation is done. Before I used to have the method:
-(void) animateMain :(NSArray*) imagesArray{
imgH6.animationImages = imagesArray;
// I will then start the animation as:
double wait = ((double)[imgH6.animationImages count])/30;
[imgH6 setAnimationRepeatCount:1];
imgH6.animationDuration = wait;
[imgH6 startAnimating];
}
and the application still crashed after playing several times. I have noticed that it crashes when changing the animation several times. In other words if I alway splay the same animation it does not crash...
also I do not used the word alloc or init to instantiate the array so I don't think this has to do with releasing the array.