Trouble using UIImageView to show animations - ios

I am an iOS beginner working through the Pragmatic iOS book, and am having trouble with one of the chapter end "challenges" where I am to show slideshow of images in the UIImageView in my nib file.
I have a model class Recipe.h, as well as a ViewController and the standard AppDelegate.h files. Everything that is all wired up correctly, and if I were to show only one image in my UIImageView via self.imageView.image = self.recipe.image, everything works correctly. However, if I were to do the following and attempt to show using animnationImages, no images show up:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//...some code here
if(self.recipe.imageArray){
self.imageView.animationDuration = 2;
self.imageView.animationRepeatCount = 2;
self.imageView.animationImages = self.recipe.imageArray;
NSLog(#"%#", self.imageView.animationImages);
}
}
My NSLog outputs the UIImage: 0x7520d60, UIImage: 0x7522390, which I take it that self.imageView.animationImages has the images located in memory. However, why are the pictures not showing up?
Thanks.

Use
[self.view startAnimating];

Related

Should I use GIF file or animation code on iOS?

I intend to make a splash screen on iOS consists of a person, a chair objects. Each of these objects has seperated aspects like the hands, head, body and feets that animated together. I wonder which is better way to go? Importing a GIF file or coding CALayer objects then adding animation?
You can't use GIF files or any code for animation for your launchscreen (splash screen), you can only use a static image, a PNG or JPG (if the launchscreen is a storyboard).
So, if you want your app's startup with some animation then you should manage it in your first view controller.
You can animate imageview with image sets like,
UIImageView* myImageViewForAnimation = [[UIImageView alloc] initWithFrame:self.view.bounds];
myImageViewForAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"],
[UIImage imageNamed:#"image4"], nil];
myImageViewForAnimation.animationDuration = 1.0f;
myImageViewForAnimation.animationRepeatCount = 0;
[myImageViewForAnimation startAnimating];
[self.view addSubview: myImageViewForAnimation];
Update (as asked in comment) :
You can get your view on which you have added gesture recognizer in your action method or you can set tag to every imageview and handle it in action method.
For example,
-(void)tapOnProfileImage : (UITapGestureRecognizer*)recog{
UIImageView *tempView = (UIImageView *)recog.view;
// or
if (recog.view.tag == 1) {
// image 1
}
if (recog.view.tag == 2) {
//image 2
}
}
So, on every image view, you should add target with selector - tapOnProfileImage and you can differentiate it as mentioned in above code snippet!
You can use gif image but not directly on launch xib. You create one view controller and load gif image on that view controller, once animation is completed then remove this controller from navigation stack. https://github.com/Flipboard/FLAnimatedImage use this gif image view control.

Zoom and Pan on a UIPageViewController

I have a simple application which consists of a UITableViewController with cells of Languages. When the user clicks a specific language, it brings up a UIPageViewController to show horizontally, 6 images in a pageView. That part is working really well and the images are loading.
I have implemented this, using this tutorial: http://code.tutsplus.com/tutorials/using-scrollstyle-with-uipageviewcontroller--mobile-13551.
I would like to achieve a zoom and pan functionality within this as well now.
I have zoom working with the following code.
CGFloat scale = pinchRecognizer.scale;
self.pageViewController.view.transform = CGAffineTransformScale(self.pageViewController.view.transform, scale, scale);
pinchRecognizer.scale = 1.0;
The problem is that the zoom is always in the centre of the image with no way to pan around the image like you can in a normal Photo on an iOS device.
I have found lots of good tutorials on how to achieve this, like http://iosdeveloperzone.com/2012/07/07/tutorial-all-about-images-part-2-panning-zooming-with-uiscrollview/ as an example. However, my confusion stems from the very first part.
I am loading my UIPageViewController using a NSArray:
self.modelArray = [NSMutableArray arrayWithObjects:[[ImageModel alloc] initWithImageName:#"3facts-english-page1.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-english-page2.jpg"], [[ImageModel alloc] initWithImageName:#"3facts-english-page3.jpg"], nil];
How to I extract each image into a UIImageView?
I can see that fundamentally, I need to have most of this code to enable the Pan:
UIImage* image = [UIImage imageNamed:#"KinkakuJi"];
self.imageView.image = image;
[self.imageView sizeToFit];
self.scrollView.contentSize = image.size;
In my case however, I'm not loading a single UIImage but rather a NSArray. How do I extract the UIImage from the NSArray to make this pan happen?
Any thoughts on this would really be appreciated.

Can't update a UIImageView properly

I call this method in my ViewController, which contains a UIScrollView, when the user taps a button.
-(void) updateView:(NSURL *) urlPicture{
UIImageView *imageNews = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,200)];
imageNews.contentMode = UIViewContentModeScaleAspectFit;
[imageNews setImageWithURL:urlPicture];
[self.scrollView addSubview:imageNews];
}
It seems to work fine, but when I got a strange behavior.
It doesn't clear the previous image to put the new one.
E.g: if the first image is 300x200 and the second one is 200x200 I still can see the sides of the first one when the app download the second.
I would like to know how to clear the previous image before download the second.
I already try imageNews.image = nil; but it didn't work
First you need to remove old view from scrollView. You can edit your code as mentioned below
#define IMAGE_VIEW_TAG 1001
-(void) updateView:(NSURL *) urlPicture{
UIView *oldView = [self.scrollView viewWithTag:IMAGE_VIEW_TAG];
if(oldView)
[oldView removeFromSuperView]
UIImageView *imageNews = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,200)];
imageNews.tag = IMAGE_VIEW_TAG;
imageNews.contentMode = UIViewContentModeScaleAspectFit;
[imageNews setImageWithURL:urlPicture];
[self.scrollView addSubview:imageNews];
}
Well I am not really sure what your application does ,
but seems like you need to have only 1 image
so I would suggest you to remove the previous image.
There are 2 ways to do so:
1) you could use your image as property and simply call [_imageNews removeFromSuperView];
2) you can iterate through your self.scrollView's sub views and remove it from there.

Upraded Xcode, I have 2 Identicle views with UIScrollView. One View adds components to UIScroll fine, the other does not

I have an app that has been working just fine for ages. I upgraded XCode and now 1 of my views is doing weird things.
None of the objects I attempt to add to the UIScrollview show up.
I have 2 screens that are basically identical in code (i literally copy and pasted). One of them works just fine, I click a button to move to the other screen, and it doesn't display ANY components I attempt to add to the scroll view.
Any ideas as to what could be wrong? Why it would suddenly stop adding components after upgrading Xcode?
I have a background image that I have tested and changed to self.view that changes just fine. But anything I try to add to my UIScrollView, nothing gets added. Some sample code...
my .h declaration
#interface OutgateInputViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate>{
IBOutlet UIScrollView *outputsView;
a sample in my .m
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"inputbackground.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
int x = 20;
int y = 20;
CGRect myImageRect = CGRectMake(0, 15, 320, 32.5);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
NSString *imgName = #"screen_logo.png";
[myImage setImage:[UIImage imageNamed:imgName]];
[outputsView addSubview:myImage];
y = y + 30;
pretty standard stuff. its an EXACT copy up to this point of the screen that works. (even a copy long after, i just pull data from a different location). So any ideas on what changed?
I went back and re-did everything from scratch, and apparently for some reason THIS View needs the additional __weak at the start of the declaration in the .h ...
no idea why as of yet, but this solved the issue of the entire view being broken.
__weak IBOutlet UIScrollView *outputsView;

How to update iOS PhotoScroller sample code

I'm using altered sample code from PhotoScroller within my app. I have a table view of image thumbnails, and I can pass the array of images that populate that table to PhotoViewController. Currently, PhotoViewController starts with the first image in my array and I can scroll back and forth. This works properly as Apple's sample code.
Now What I want to do is tap a table cell with thumbnail, and start scrolling images beginning with the image in my array at that index. Ex: if I have 5 images in a table and I tap image #3, I want the first image in PhotoViewController to be that third image, and able to scroll left or right to #2 or #4. Hope this makes sense.
I see in PhotoViewController that sub views are being added per image. Any way I can tell it "jump to view #3" without destroying the other views or their overall order of appearance? Any ideas or advice is welcome. Code can be found on the iOS developer site for PhotoScroller sample code.
Ok, I'm rambling... Thanks in advance for your help!
The way I do this is to have a variable called startingPage which gets set in the initialiser of the photo view controller. Then when the pages are being created, first set the correct offset for the scroll view.
So in the PhotoScroller case that would be in loadView. Like so:
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
self.view = pagingScrollView;
// Set the content offset of the scrollview
CGRect bounds = pagingScrollView.bounds;
CGPoint contentOffset = CGPointMake(bounds.size.width * startingPage, 0.0f);
[pagingScrollView setContentOffset:contentOffset animated:NO];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}

Resources