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];
Related
1) In my application, I want to zoom and pinch an image. In xib file, there is a imageView inside a scrollview.Page control is used for swiping images. For a single image it gets zoomed, but for multiple images(coming from web service) it is not getting swipe and zoom.
In setMultipleImages method, images are not getting zoomed and swiped, while if image is single it gets zoom properly.
2) In UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]]; line code, want to replace uiimage with uiimageview. How can I do it?
My code is:
-(void)setMultipleImages{
for (int i = 0; i < [imageMArray count]; i++){
NSString *logoName;
NSArray *imageNameArray = [[imageMArray objectAtIndex:i] componentsSeparatedByString:#"/"];
logoName = [[NSString alloc]initWithFormat:#"%#",[imageNameArray lastObject]];
if (![[AppDelegateHelper getInstance] fileExist:logoName])
{
NSString *imageURL = [[NSString alloc]initWithFormat:#"%#%#",[UrlBuilder getWebsiteUrl],[imageMArray objectAtIndex:i]];
[self startProcessing];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[activityIndicator stopAnimating];
DebugLog(#"%f,%f",image.size.width,image.size.height);
NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [docDir objectAtIndex:0];
DebugLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#",cacheDirectory,[imageNameArray lastObject]];
NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data1 writeToFile:pngFilePath atomically:YES];
}
}
[self loadImageData];
}
-(void)loadImageData
{
int X =0;
for (int s=0; s<imageMArray.count; s++) {
NSString *logoName;
NSArray *imageNameArray = [[imageMArray objectAtIndex:s] componentsSeparatedByString:#"/"];
logoName = [[NSString alloc]initWithFormat:#"%#",[imageNameArray lastObject]];
if([[AppDelegateHelper getInstance] loadImage:logoName] != nil)
{
// UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;
[imageView setImage:[[AppDelegateHelper getInstance] loadImage:logoName]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview: imageView];
X = X + imageView.frame.size.width;
}
else
{
//UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;
[imageView setImage:[UIImage imageNamed:#"NoImage.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview: imageView];
X = X + imageView.frame.size.width;
}
}
self.pageControl.numberOfPages = [imageMArray count];
scrollView.contentSize = CGSizeMake(X,scrollView.frame.size.height);
lblCount.text = [[NSString alloc]initWithFormat:#"1 sur %lu",(unsigned long)imageMArray.count];
}
Add all your images to imageview. Then apply pinchguesture to images view for zoom effect.
Pinch Guesture:
UIPinchGestureRecognizer *imageviewguesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(ImageViewPinchGuesture:)];
add pinch guesture to your image view:
[YourImageView addGestureRecognizer:imageviewguesture];
Make your selector method:
-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer{
pinGuestureregonizer.view.transform = CGAffineTransformScale(pinGuestureregonizer.view.transform, pinGuestureregonizer.scale, pinGuestureregonizer.scale);
pinGuestureregonizer.scale = 1;
}
In your .h file add this line
-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer;
To check effect in simulator Press Cmd+alt then press left mouse pad button and drag.
Hope this will Help. Don't forget to accept the answer if it helps you.
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];
I want to create a video with mp3 using NSMutableArray of Images.I have Array of multiple images and i want to create a video with custom mp3 file (user selected mp3).
I think this link is usefull for you..
GitHub Link
Use the Simple Animation Using UIImageView and play thw selected mp3 file as background music using AVAudioPlayer .
//Animation
// 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];
//background music
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Soothing_Music2_Long" ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
I am working on an app in which I need to load nearly 211 images in a scrollview.
What I was doing was I was converting the images in binary format (NSData) and saving them in core data. Then retrieving them and populating them on the scrollview.
it does work, but sometimes it does throw "Memory warning".
I learned that lazy loading is a way to achieve this. But, I am not totally aware of how it is done.
Like, loading 3 or 5 images ahead/back of the image visible currently on the scrollview.
Can any bode help me with a simple example, which would help me understand from scratch ? and would point me in the right direction.
Thank you for your time.
Well, it is not needed to store images into core data. Just take all image URLs into NSMutableArray and try to use following code as per your requirement.Hope this helps you.
Try this code -
friendsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 470, 320, 100)];
friendsScrollView.contentSize = CGSizeMake([meetUPFrndNameArray count] * 95,50);
friendsScrollView.backgroundColor = [UIColor clearColor];
friendsScrollView.delegate = self;
[self.view addSubview:friendsScrollView];
int imageX = 25,imageY = 20;
int backImageX = 10, backImageY = 10;
int flag = 0;
for (int i = 0; i < [meetUPFrndPhotoArray count]; i++) {
flag ++;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[flag-1]];
UIImage *img1 = [UIImage imageWithContentsOfFile:savedImagePath];
if (!img1 || [UIImagePNGRepresentation(img1) length] <=0)
{
id path = meetUPFrndPhotoArray[flag-1];
path = [path stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url = [NSURL URLWithString:path];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:url, [NSString stringWithFormat:#"%d", flag], nil];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)];
frndBackgroundImage.image = [UIImage imageNamed:#"friend_image_background.png"];
frndBackgroundImage.backgroundColor = [UIColor clearColor];
frndBackgroundImage.layer.borderWidth = 2.0;
frndBackgroundImage.layer.masksToBounds = YES;
frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1);
frndBackgroundImage.layer.shadowOpacity = 1;
frndBackgroundImage.layer.shadowRadius = 1.2;
frndBackgroundImage.layer.cornerRadius = 5.0;
frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor];
[friendsScrollView addSubview:frndBackgroundImage];
UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)];
friendImage.tag = flag;
friendImage.image = [UIImage imageNamed:#"ic_user.png"];
[friendsScrollView addSubview:friendImage];
}
else
{
UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)];
frndBackgroundImage.image = [UIImage imageNamed:#"friend_image_background.png"];
frndBackgroundImage.backgroundColor = [UIColor clearColor];
frndBackgroundImage.layer.borderWidth = 2.0;
frndBackgroundImage.layer.masksToBounds = YES;
frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1);
frndBackgroundImage.layer.shadowOpacity = 1;
frndBackgroundImage.layer.shadowRadius = 1.2;
frndBackgroundImage.layer.cornerRadius = 5.0;
frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor];
[friendsScrollView addSubview:frndBackgroundImage];
UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)];
friendImage.tag = flag-1;
friendImage.image = img1;
[friendsScrollView addSubview:friendImage];
}
backImageX = backImageX + 80 + 10;
backImageY = 10;
imageX = imageX + 50 + 25 + 15;
imageY = 20;
}
Here meetUPFrndPhotoArray contains image URLs.
And
For Downloading images in Background -
- (void) loadImageInBackground:(NSArray *)urlAndTagReference{
NSData *imgData = [NSData dataWithContentsOfURL:urlAndTagReference[0]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:img, urlAndTagReference[1], nil];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
}
For Assigning downloaded image to perticular imageView -
- (void) assignImageToImageView:(NSArray *)imgAndTagReference{
for (UIImageView *checkView in [friendsScrollView subviews] ){
if ([imgAndTagReference count] != 0) {
if ([checkView tag] == [imgAndTagReference[1] intValue]){
[checkView setImage:imgAndTagReference[0]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[[imgAndTagReference[1] intValue]-1]];
UIImage* imageToSave = [checkView image];
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToFile:savedImagePath atomically:NO];
}
}
}
}
Let me know if this helps you.Thanks in advance.All the best.
Make use of delegate functions, content size and content inset properties of the scrollview
Checkout this library It's a generic UIScrollView that reuses it's subviews like UITableView does (there is a dataSource object which is used to configure scroll view subviews)
This is what I currently have:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [NSString stringWithFormat:#"dance2_%03d.jpg", count];
UIImage *frame = [UIImage imageNamed:fileName];
[images addObject:frame];
}
UIImage imageNamed is causing me some memory issues and I would like to switch to imageWithContentsOfFile.
I can make it work with a single image, but not the whole array:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/dance2_001.jpg"];
UIImage *frame = [UIImage imageWithContentsOfFile:fileName];
[images addObject:frame];
}
Any help is greatly appreciated! Thanks!
for(int i = 1; i <= 21; i++)
{
[images addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"dance2_%03d", i] ofType:#"jpg"]]];
}
what you should do first is create an array with the images for your animation like something like this:
NSMutableArray* images = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image1" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image2" ofType:#"jpg"]],
nil];
then you can add it to an UIImageView to animate it like this:
UIImageView* animationImagesView = [[UIImageView alloc] initWithFrame:CGRectMake(posX, posY, frameWidth, frameHeight)];
animationImagesView.animationImages = images; //array of images to be animate
animationImagesView.animationDuration = 1.0; //duration of animation
animationImagesView.animationRepeatCount = 1; //number of time to repeat animation
[self.view addSubview:animationImagesView];
now you can start and stop the animation using these two calls:
[animationImagesView startAnimating]; //starts animation
[animationImagesView stopAnimating]; //stops animation
hope this helps. also remember to release and nil your Array and UIImageView when done.