How can I make this GIF appear correctly? - ios

I created a GIF and it is basically snow particles falling and has a transparent background. Therefore I can have just snow flowing over my other PNGs and images on the app. The problem is that the app loads and the GIF appears all white. Not sure why because I gave it transparency when making it. If I lower the alpha in Xcode I still don't see my old PNGs (usual background image).
This is in my ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"snow" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
[webViewBG loadData:gif MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
webViewBG.userInteractionEnabled = NO;
[self.view addSubview:webViewBG];
UIView *filter = [[UIView alloc] initWithFrame:self.view.frame];
//filter.backgroundColor = [UIColor redColor];
filter.alpha = 0.5;
[self.view addSubview:filter];

You can set your view background Clear color And for animated image you can use UIImageView.
hope this will be work proper.
See Link:: - Click here to get code
Link2::- Click here for code

Related

memory issue for image animation with GIF - ios

I am using the following code to show the splash screen :
UIImageView *defaultImage = [[UIImageView alloc] init];
defaultImage.frame = defaultImageFrame;
// for iOS 9 Compatability
//NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *buttonsArray = [NSMutableArray arrayWithCapacity:1];
for (int i = 1; i<=3; i++)
{
[buttonsArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"img00%d", i] ofType:#"png"]]];
}
mAnimatedButtons = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_DEVICE, HEIGHT_DEVICE)];
[mAnimatedButtons setUserInteractionEnabled:NO];
[mAnimatedButtons setAnimationImages:buttonsArray];
[mAnimatedButtons setAnimationRepeatCount:0];
[mAnimatedButtons setAnimationDuration:2.0];
[mAnimatedButtons startAnimating];
[defaultImage addSubview:mAnimatedButtons];
[self.view addSubview:defaultImage];
And i will remove the splash screen after this
[mAnimatedButtons stopAnimating]; //here animation stops
[mAnimatedButtons removeFromSuperview]; // here view removes from view hierarchy
mAnimatedButtons = nil;
self.defaultImage=nil;
[self.defaultImage removeFromSuperview];
[self.view.layer removeAllAnimations];
for (CALayer* layer in [self.view.layer sublayers])
{
[layer removeAllAnimations];
}
Adding this code increases the memory usage upto 300 Mb.
Removing this it has only 100 Mb.
I tried the following code also
self.defaultImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_DEVICE, HEIGHT_DEVICE)];
self.defaultImage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img001.png"],
[UIImage imageNamed:#"img002.png"],
[UIImage imageNamed:#"img003.png"],
nil];
//self.defaultImage.animationDuration = 1.0f;
self.defaultImage.animationRepeatCount = 0;
[self.defaultImage setAnimationDuration:2.0];
[ self.defaultImage startAnimating];
[self.view addSubview: self.defaultImage];
Even though the result is same.
Images, when used in the app, may require considerably more memory than the size of the asset in persistent storage might otherwise suggest. Assets are frequently compressed (e.g. JPG or PNG), but when you use the image, they're uncompressed, often requiring 4 bytes per pixel (one byte for red, green, blue, and alpha, respectively). So, for example, a iPhone 7+ full-screen retina image can require 14mb when you use the image. So, the memory-efficient technique is to employ lazy loading, not creating the UIImage objects until you absolutely need them. And, as Jerry suggested, because the amount of memory is determined by the size of the image and not the imageview in which you use the image, if your images have dimensions greater than required by the UIImageView in which you use them (i.e. width and height of the imageview times the "scale" of the device), you may want to resize the image accordingly.

How to load gif image and control that in ios?

In my application i want load the gif images in my view and also controls stop and continue animating.
Kindly follow the below tutorial that may help you.
http://anand3777.blogspot.in/2014/07/gif-image-loading.html
Step 1:
Download library from below url and add it on your project.
https://drive.google.com/folderview?id=0B5JC34Lt79ctamtOVTV6SHhzOVU&usp=sharing
Step 2:
Import the file on your "viewController.h" like below.
//gifImage//
#import "SCGIFImageView.h"
//gifImage//
Step 3:
Create object like given below on your "viewController.h".
//gifImage//
IBOutlet SCGIFImageView* _gifImageView;
IBOutlet UIButton* _button;
//gifImage//
Step 4:
Create ibaction on your "viewController".
//gifImage//
- (IBAction)controlAnimate:(id)sender;
//gifImage//
Step 5:
Add the following code were you want to use gif image
//gifImage//
//load url gif image
NSURL *imageURL = [NSURL URLWithString:#"http://google.co.in/anim.gif"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
/*
//load local gif image
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"1.gif" ofType:nil];
NSData* imageData = [NSData dataWithContentsOfFile:filePath];
*/
_gifImageView = [[SCGIFImageView alloc] initWithFrame:CGRectMake(225, 70, 75, 75)] ;
[_gifImageView setData:imageData];
[self.view addSubview:_gifImageView];
//gifImage//
Step 6:
Add the following code for stop/start animating
//gifImage//
- (IBAction)controlAnimate:(id)sender{
_gifImageView.animating = !_gifImageView.animating;
if (_gifImageView.animating) {
[_button setTitle:#"Pause" forState:UIControlStateNormal];
} else {
[_button setTitle:#"Continue" forState:UIControlStateNormal];
}
}
//gifImage//
[1]: https://drive.google.com/folderview?id=0B5JC34Lt79ctamtOVTV6SHhzOVU&usp=sharing
Use Latest GIF Class from https://github.com/Flipboard/FLAnimatedImage
It is easy to use and memory friendly.
In your code, #import "FLAnimatedImage.h", create an image from an animated GIF, and setup the image view to display it:
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"]]];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0); //As your Wish you can set frame
[self.view addSubview:imageView];
or If you are using Bundle gif file then
NSURL *url = [[NSBundle mainBundle] URLForResource:#"loading_01" withExtension:#"gif"];
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:url]];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0); //As your Wish you can set frame
[self.view addSubview:imageView];

Adobe Edge animation on IOS7 not working

I'm trying to implement an Adobe Edge animation inside a UIWebView in my IOS7 app. But all I get is a white screen. The same white screen shows up on my iPhone's Safari. The animation runs fine on my laptop's browsers, both Safari and Chrome.
I'm thinking this is an Adobe Edge problem, but here's my IOS code anyway:
NSString *filePathString = [[NSBundle mainBundle] pathForResource:#"YallaBye_Button2" ofType:#"html"];
NSMutableString *html = [[NSMutableString alloc] initWithContentsOfFile:filePathString encoding:NSUTF8StringEncoding error:nil];
NSURL *aURL = [NSURL fileURLWithPath:filePathString];
UIWebView *view = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
view.delegate = self;
[view stringByEvaluatingJavaScriptFromString:#"window.requestAnimationFrame = window.setTimeout;"];
[view loadHTMLString:html baseURL:aURL];
[self.indicatorForPlans addSubview:view];
The indicator works fine without the Adobe Edge html.
I also tried running the [view stringByEvaluatingJavaScriptFromString:#"window.requestAnimationFrame = window.setTimeout;"]; call inside the UIWebViewDelegate method - (void)webViewDidFinishLoad:(UIWebView *)webView but no luck.
Also, researching the forums, I tried changing my transform:translateX(0px); to -webkit-perspective: 1000; -webkit-backface-visibility: hidden; but still just a white screen.
What am I missing?

Programmatically placing an image in an UIImageView on a UIViewController

I have an image (a .png file) that I want to place in an ImageView in a ViewController. I use the following code but the simulator gives me a blank white view without the image. The .png file is in the same directory as the ViewController files. Here is the code:
#implementation ViewController
{
NSArray *_pArray;
UIImage *_image;
UIImageView *_imageView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_image = [[UIImage alloc] initWithContentsOfFile:#"TM-1P2.png"];
_imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[_imageView setImage:_image];
[_imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:_imageView];
}
If you examine at _image (either NSLog or in the debugger), it probably is nil. With initWithContentsOfFile you should specify the entire path, for example:
NSString *path = [[NSBundle mainBundle] pathForResource:#"TM-1P2" ofType:#"png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];
Alternatively, you can use the following, which automatically looks for the image in the bundle:
_image = [UIImage imageNamed:#"TM-1P2.png"];
This latter syntax, imageNamed, caches the image (i.e. will keep it in memory even if you dismiss the view controller). That's great if you have to use the same image again and again throughout the app (because it won't have to reload it every time), but if you only use it once, you might not want to use imageNamed. As the imageNamed documentation says:
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
Note, both of these assume that you've successfully added this image to your bundle.
If, on the other hand, the image is in your Documents folder, you could load it like so:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentsPath stringByAppendingPathComponent:#"TM-1P2.png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];
Finally, note that the iOS devices are case sensitive (generally the simulator is not), so make sure you have your capitalization correct.
Unrelated to your question, those variables in between the braces probably should not be defined in the #implementation, but rather you should put them in a #interface. For example, you could put them in your .h file, or better, you can put them in a private class extension in your .m file, right before the #implementation:
#interface ViewController ()
{
NSArray *_pArray;
UIImage *_image;
UIImageView *_imageView;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"TM-1P2" ofType:#"png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];
_imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[_imageView setImage:_image];
[_imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:_imageView];
}
// ...
#end
Have you stepped through in the debugger?
I would be interested to see if _image is non-nil - and the most likely reason for it being nil is that you have not added it to your project.
If you have your image named as "TM-1P2.png" in your bundle, you can simply do the following:
_image = [UIImage imageNamed:#"TM-1P2.png"];
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:#"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}

Enable close button in UIWebView

I need to display webview in cocos2D game. I used this code. But it is not displaying close button, how can I enable close button? Here is screen that I want:
Code Used:
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView* myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
myWebView.backgroundColor = [UIColor whiteColor];
myWebView.scalesPageToFit = YES;
myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
myWebView.delegate = self;
[self.navController.view addSubview:myWebView];
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://biggooseegg.com/moregamesscreen"]]];
Some other doubts: How can I get rounded border for this web view?
Here is my sample that not completed.
The close button isn't part of UIWebView, you'll have to add it yourself.
Rounded corners can be achieved by displaying said rounded corners background image behind the UIWebView. You can use a CCSprite for that. Then make the web view fit inside the background image.

Resources