Adding animation at start up using an array of images - iphone

I'm new to iphone app development and I'm trying to add an image to the start of my app. I've created an array of images which when iterated should create an animation.
-(void)viewDidLoad
{
animation = [[UIImageView alloc] init];
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"panda1.png"],
[UIImage imageNamed:#"panda2.png"],
[UIImage imageNamed:#"panda3.png"], nil];
animation.animationRepeatCount = 2;
animation.animationDuration = 2;
[animation startAnimating];
[self.view addSubview:animation];
[animation release];
//for loading the webpage at start up
NSString *urlAddress = #"http://projects.seng.uvic.ca/fatpanda/m/most_recent.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the request in the UIWebView
[webView loadRequest:requestObj];
}
The reason I release the animation at the end of this is because I'd like to remove the last image so I can replace it with a view of a website in a UIWebView and have that visible at the end. The code above compiles but when I run it no images appear but the website does.
Any help would be greatly appreciated.

try this, i think your frame is ending up all zeros.
UIImage * first = [UIImage imageNamed:#"panda1.png"]
animation = [[UIImageView alloc] initWithImage:first];
animation.animationImages = [NSArray arrayWithObjects:
first,
[UIImage imageNamed:#"panda2.png"],
[UIImage imageNamed:#"panda3.png"], nil];
Also...
releasing the animaiton will not remove it from the superview. in fact calling addSubview will increase the reference count to the UIImageView and releasing it is the correct thing to do. When you want to to remove from the view it you will have to find it again in the list of subviews and either hide it or call removeFromSuperview.

Related

UIWebview scalesPageToFit doesn't work perfectly

I'm using a small UIWebView, and scalesPageToFit doesn't work correctly for some url. (look like it cannot resize at 100%. (but if i zoom out the webview manually(gesture), it work!
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 400.0f)];
webViewT = [[UIWebView alloc] initWithFrame:view.bounds];
webViewT.delegate = self;
webViewT.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:#"http://www.google.com/"];
[webViewT loadRequest:[NSURLRequest requestWithURL:url]];
Please note that i've found a solution:(below work, BUT I have the time to see a "resize"
- (void) webViewDidFinishLoad:(UIWebView *)webViewT {
if ([theWebView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[theWebView scrollView];
float zoom=theWebView.bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:NO];
}
}
Any idea? Thanks!
In Some of cases this types of problem Generated. At that time java script use for control of Zoom of UIWebView, use following code.
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Other way is
Refer this Answer UIWebView does not scale content to fit

UIWebview fitting to screen but not zooming in iphone programming

I am working on uiwebview and it is supposed to load dynamic urls coming from the server.
i want to show it fitting to the iPhone screen size in my iPhone application and also it should zoom in and out.
Code i am using for it is:
m_pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
m_pView.backgroundColor = [UIColor blackColor];
self.view = m_pView;
UIWebView *aWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 1, 320, 400)] autorelease];
aWebView.dataDetectorTypes = UIDataDetectorTypeNone;
aWebView.delegate = self;
NSString* url = [NSString stringWithFormat: #"http://%#",[m_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.frame = self.view.bounds;
NSURL *aURL = [NSURL URLWithString: url];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
//load the index.html file into the web view.
[aWebView loadRequest:aRequest];
[m_pView addSubview:aWebView];
self.m_webView = aWebView;
Please let me know how to achieve webview fitting to the iphone screen size without loosing zoom in/out feature.
Thanks in advance.
Use following code
yourWEbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
yourWEbView.contentMode = UIViewContentModeScaleAspectFit;
problem might be in this line:
aWebView.frame = self.view.bounds;
Why you're setting bounds of self.view while adding webview to m_pView?

Is it possible to animate an UIImage?

I'm wondering if there's anyway to animate an UIImage.
I know that UIImageViews are possible to animate but is there any way to do it directly in a UIImage.
Maybe with Open GL ES or something?
Or are there any other ways you can animate an MPMediaItemArtwork?
Thanks in advance!
Create a UIImageView and set the property of animationImages to an array of UIImages
Here is an example:
NSArray *animationFrames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"image1.png"],
[UIImage imageWithName:#"image2.png"],
nil];
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = animationsFrame;
[animatedImageView startAnimating];
If you're targeting iOS 5 you can do this directly in UIImage without the UIImageView using
+(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
for example,
[UIImage animatedImagesWithImages:animationFrames duration:10];
If you mean animation with a few images, use this code:
// create the view that will execute our animation
UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
YourImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"01.gif"],
[UIImage imageNamed:#"02.gif"],
[UIImage imageNamed:#"03.gif"],
[UIImage imageNamed:#"04.gif"],
[UIImage imageNamed:#"05.gif"],
[UIImage imageNamed:#"06.gif"],
[UIImage imageNamed:#"07.gif"],
[UIImage imageNamed:#"08.gif"],
[UIImage imageNamed:#"09.gif"],
[UIImage imageNamed:#"10.gif"],
[UIImage imageNamed:#"11.gif"],
[UIImage imageNamed:#"12.gif"],
[UIImage imageNamed:#"13.gif"],
[UIImage imageNamed:#"14.gif"],
[UIImage imageNamed:#"15.gif"],
[UIImage imageNamed:#"16.gif"],
[UIImage imageNamed:#"17.gif"], nil];
// all frames will execute in 1.75 seconds
YourImageView.animationDuration = 1.75;
// repeat the animation forever
YourImageView.animationRepeatCount = 0;
// start animating
[YourImageView startAnimating];
// add the animation view to the main window
[self.view addSubview:YourImageView];
Source
Check UIImage's +animatedImageWithImages:duration: and +animatedImageNamed:duration:.
From UIImage Class Reference:
Creates and returns an animated image from an existing set of images.
This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image’ as its contents, this method would attempt to load images from files with the names ‘image0’, ‘image1’ and so on all the way up to ‘image1024’. All images included in the animated image should share the same size and scale.
You can use this.
arrayOfImages=[[NSMutableArray alloc]init];
for(int i=1;i<=54;i++)
{
NSString *nameResources=#"haKsequence.png";
NSString *changedString= [NSString stringWithFormat:#"%d", i];
NSString *stringWithoutSpaces = [nameResources stringByReplacingOccurrencesOfString:#"K" withString:changedString];
[arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage];
}
self.view.userInteractionEnabled=NO;
i1.hidden=YES;
image1=[[UIImageView alloc]init];
image1.backgroundColor=[UIColor clearColor];
image1.frame = button.frame;//i1 is obshaped buttion
[self.view addSubview:image1];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1;
animation.values = arrayOfMonkeyImages;
[animation setDelegate:self];
animation.repeatCount=3;
[animation setRemovedOnCompletion:YES];
[image1.layer addAnimation:animation forKey:#"animation"];
Swift 4 version:
let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()

UIImageView animation has long delay on device

I create a 5-frame animation on UImageView and each image is created by [UIImage imageWithContentOfFile:xxx],
however, the animation on simulator is working well but not on device, it usually has ~1 sec delay though the animation duration is 1sec.
This is my code.
NSArray *animations = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"ShakeAnimation"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:[animations objectAtIndex:0]]];
imgView.tag = 10;
NSMutableArray *animationImgs = [[NSMutableArray alloc] init];
for( NSString *path in animations )
{
[animationImgs addObject:[UIImage imageWithContentsOfFile:path]];
}
[imgView setAnimationRepeatCount:2];
[imgView setAnimationImages:animationImgs];
[imgView setAnimationDuration:1];
[animationImgs release];
and when the view controller recieved shack event, it calls [imgView startAnimation];
Is anyway to preload the images involved in the animation? thanks.
I figured it out!
If you want to preload the UIImageView animation images, just call [imgView startAnimating] after initialization. It seemed to work for me.

Anti-aliasing not working when resizing a UIWebView

I'd like to add a Web View to my app at 60% scale (like seen in Safari in the browse other windows view):
Notice how the content looks nice and Aliased!
If I try and add the same Web view to my app:
NSURL *url = [NSURL URLWithString:#"http://www.google.co.uk?q=hello"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
webView.delegate=self;
[webView loadRequest:request];
[self.view addSubview:webView];
Using the following transformation:
[webView setTransform:CGAffineTransformMakeScale(0.6, 0.6)];
..the scale is really bad quality and there appears to be no anti-aliasing.
Does anyone know why this is happening or have a suggestion on how it could be fixed?
Thanks!
Nick.
I figured out a solution!
Take a snapshot of the webview, add it to a UIImageView and resize that insted!
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(webView.bounds.x, webView.bounds.y, webView.frame.size.width, webView.size.height)];
imageView.image = viewImage;
[self addSubview:imageView];
[imageView release];
webView.hidden = YES;
Seems to work fine!
One point to note.. if you want to do this as soon as the WebView has loaded, you need to defer the action by some period (I chose a 10th of a second!)
Like so:
-(void)deferLoading
{
webViewHasActuallyLoaded = YES;
[self setNeedsLayout];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[NSTimer scheduledTimerWithTimeInterval: 1.0/10.0 target:self selector:#selector(deferLoading) userInfo:nil repeats:NO];
}
Cheers for suggestions abovee!
Nick.
Why not just make the web view smaller? It should render the content in high quality at the smaller size.
If you need to animate the change in size, you can set the minificationFilter on the web view's layer to get reasonable quality:
[[webView layer] setMinificationFilter:kCAFilterLinear];
In your UIViewController override
(void)placeViewItems: (UIInterfaceOrientation)interfaceOrientation
And make your UIWebView's frame.origin.x & frame.origin.y integer values.
See if that is the result you are looking for.