Fade in and out between images in NSmutableArrray - ios

I would like to know based on my code, how can i add fade in fade out effect between each images in my NSArray. As i am using these images as background. Changing background without fade in and out looks horrible.
Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *imageNames = #[#"1.jpg", #"2.png", #"3.png", #"4.png",
#"5.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animationImageView.animationImages = images;
animationImageView.animationDuration = 9;
[self.background addSubview:animationImageView];
[animationImageView startAnimating];}

Related

Image Iteration in iOS

Is there a better way to iterate the following images?
NSArray *imageNames = #[#"MyImage-0.png", #"MyImage-1.png", #"MyImage-2.png",#"MyImage-3.png",#"MyImage-4.png", #"MyImage-5.png", #"MyImage-6.png", #"MyImage-7.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
mImageView.animationImages = images;
mImageView.animationDuration = 1.5;
[self.view mImageView];
[mImageView startAnimating];
NSInteger imagesCount = 7;
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i <= imagesCount; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:#"MyImage-%d", i]]];
}
mImageView.animationImages = images;
mImageView.animationDuration = 1.5;
[self.view mImageView];
[mImageView startAnimating];
You can use enumeration
for(NSString *imageStr in imageNames)
{
[images addObject:[UIImage imageNamed:imageStr]];
}
This takes less time to iterate.
Hope it helps you...!
The better way will be not using for loop. Try this code
+(NSArray*)getSortedArray:(NSMutableArray*)arrayToSort
{
return [arrayToSort sortedArrayUsingSelector:#selector(compare:)];
}
Hope this class method will help you.
You might want to use some handy library, like this one. Look, it even supports GIF!
NSString *urlString = #"http://raphaelschaad.com/static/nyan.gif";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
[self.view addSubview:imageView];

Animated gif in UIImageView

I want to show an animated ".gif" in a ImageView.
Now I managed it like here: click
with UIImage+animatedGIF. But if it begins to animate 2 gifs it uses 45Mb of memory I think that is very much for 2 gif's with 100kb. What can I do?
Code to start animation here:
NSString *path = [[NSBundle mainBundle]pathForResource:#"animation" ofType:#"gif"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
_testImage.image = [UIImage animatedImageWithAnimatedGIFURL:url];
_test1.image = [UIImage animatedImageWithAnimatedGIFURL:url];
I had same issue while adding animating images on UIScrollView.
Use FLAnimatedImageView.
https://github.com/Flipboard/FLAnimatedImage
IT works like magic. :)
Try this
// Load images
NSArray *imageNames = #[#"win_1.png", #"win_2.png", #"win_3.png", #"win_4.png",
#"win_5.png", #"win_6.png", #"win_7.png", #"win_8.png",
#"win_9.png", #"win_10.png", #"win_11.png", #"win_12.png",
#"win_13.png", #"win_14.png", #"win_15.png", #"win_16.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 0.5;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];

unable to create multiple UIImageView in loop ios

I am trying to show no. of images in different image view. if i have 3 image then i want show in 3 diff. UIImageView in my View. Here is my code that only shows one image. What have i missed here?
NSArray * _thumbnails = _claimReport.photos.allObjects;
if (_thumbnails.count >0)
{
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for (int i =1; i<_thumbnails.count; i++)
{
UIImageView *newImageViews = [[UIImageView alloc]initWithFrame:CGRectMake(40, 1700*i+20, 670, 700)];
LAPhoto *photo = [_thumbnails objectAtIndex:i];
[newImageViews setImage:[UIImage imageWithData:photo.photo]];
NSLog(#" newImageViews %# ",newImageViews);
[imageViews addObject:newImageViews];
[self.formView addSubview:newImageViews];
}
NSLog(#"Count %d" , imageViews.count);
}
You can add the images in a scroll view:
self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.formView addSubview:self.scrollView];
NSArray * _thumbnails = _claimReport.photos.allObjects;
if (_thumbnails.count >0)
{
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for (int i =0; i<_thumbnails.count; i++)
{
UIImageView *newImageViews = [[UIImageView alloc]initWithFrame:CGRectMake(40, 800*i+20, 670, 700)];
LAPhoto *photo = [_thumbnails objectAtIndex:i];
[newImageViews setImage:[UIImage imageWithData:photo.photo]];
NSLog(#" newImageViews %# ",newImageViews);
[imageViews addObject:newImageViews];
self.scrollView.contentSize = CGSizeMake(670, 800 *i + 800);
[self.scrollView addSubview:newImageViews];
}
NSLog(#"Count %d" , imageViews.count);
}

Changing an UIScrollview method to allow loading of views with images

How would you change this method to allow images to be in via an array rather than colors? As I have mySQL database which passing in urls which are assigned as NSStrings in xcode which are stored in an array.
Idea being that each view has set background image from the mySQL database.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor],
[UIColor greenColor],[UIColor yellowColor] , nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.size = self.scrollView.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
I made a try with (introNames is class where id and names of images are held from the mySQL database. The imageArray was created in the above code class and nsstrings from introNames were assigned to it).
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 0; i < imageArray.count; i++) {
introImages *snap = [imageArray objectAtIndex:i];
imageName2 = snap.imageName;
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.size = self.scrollView.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *subview = [[UIView alloc] initWithFrame:frame];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName2]];
[subview addSubview:backgroundView];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height);
}
This is the working data retrieval system.
-(void) retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
if (data) {
jsonConnection = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imageArray = [[NSMutableArray alloc]init];
for (int i = 0; i < jsonConnection.count; i++)
{
NSString *pID = [[jsonConnection objectAtIndex:i] objectForKey:#"id"];
NSString *pName = [[jsonConnection objectAtIndex:i] objectForKey:#"ImagesURL"];
introImages *myImages = [[introImages alloc]initWithimageID:pID andimageName:pName];
[imageArray addObject:myImages];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"Something is wrong with your internet connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Any ideas would be great :)
If you are not using iOS6+ in you app use this class PSTCollectionView
and if you are using iOS6 and above use UICollectionView Class Reference

UIImageView won't animate, static image only

I have a UIImageView on my storyboard, which is linked to an outlet in the code. When I run the code, it only shows the static picture that the storyboard shows, not the animation that I create programmatically. Any ideas?
for(int i=0; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"loading%d",i]];
[self.images addObject:image];
}
self.loadingView.animationImages = self.images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Note: loadingView is the UIImageView, images is an NSMutableArray.
If you want to make run time array you can use like this way.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 1 ; i < 4; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat: #"loading%d.jpg", i]]];
}
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Hope this will help you. It's working fine for me.
I recommend you to use array directly like this.
NSArray *images = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
nil];
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];

Resources