Received memory warning, while using animations in UIImageview like [smyImageView startAnimating]; - iphone

//NsMutableArray
//Received memory warning, while using animations in UIImageview like;
self.imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];
// Build array of images, cycling through image names
if (![self.imageArray count]>0) {
for (int i = 0; i < IMAGE_COUNT; i++){
if (i<10) {
[self.imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image000%d.jpg", i]]];
}else {
[self.imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image00%d.jpg", i]]];
}
}
}
/////Received memory warning, while using animations in UIImageview
self.animatedImages.animationImages = [NSArray arrayWithArray:self.imageArray];
[self.imageArray release];
self.imageArray=nil;
// One cycle through all the images takes 1.5 seconds
self.animatedImages.animationDuration = 0.8;
// Repeat forever
self.animatedImages.animationRepeatCount = -1;
[self.animatedImages startAnimating];

Replace
[UIImage imageNamed:[NSString stringWithFormat:#"image000%d.jpg", i]]
with
[UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:#"image000%d.jpg", i] ofType:nil]]];
because the API imageNamed: cache data in memory.

Related

How to Importing image from server and arrange it in a view

I need to get array of images that have to call from server and arrange that in an order.But while executing below code i can't get it...Guidance please...
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString:
#"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
[NSURL URLWithString:#"http://challenge.roadef.org/2012/img/google_logo.jpg"],
[NSURL URLWithString:#"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
[NSURL URLWithString:#"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
[NSURL URLWithString:#"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
[NSURL URLWithString:#"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg"],Nil];
/*
int i=0;
int cord_x=0;
int cord_y=30;
int cont=0;
for (i = 0; i < [(NSArray*)url count]; i++) {
cont=cont+1;
if (cont > 3) {
cord_x=0;
cord_y=cord_y+110;
cont=1;
}*/
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url objectAtIndex:index]]];
//cord_x=cord_x+110;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setImage:(UIImage*)[image objectAtIndex:index] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
//}
now i need to place button image and image from server...
You can try to use NSURLConnection to download images asynchronous which will make your UI much more responsive. Just setup separate connection for each image and update images on the buttons in delegate method - connectionDidFinishLoading. It will also help you to track downloading errors (e.g. timeouts etc.).
Here is a great example with downloads images for table - you can find there a lot of help for your case:
http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html
int row = 0;
int column = 0;
for(int i = 0; i < url.count; ++i) {
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*) [(NSArray*)url objectAtIndex:i]]];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+16, row*90, 80, 80);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
NSMutableArray *arrayList=[[NSMutableArray alloc] initWithObjects:#"www.xxx.com/imag.jpg",#"www.xxx.com/img1.jpg",nil];
for(int i=0; i<[arrayList count]; i++)
{
NSURL *url = [NSURL URLWithString:[arrayList ObjetAtIndex:i]];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage *img=[UIImage imageWithData:data]; // Here is your image
}
NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString:#"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"],[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],Nil];
int i=0;
int cord_x=0;
int cord_y=30;
int cont=0;
for (i = 0; i < [(NSArray*)url count]; i++)
{
cont=cont+1;
if (cont > 3)
{
cord_x=0;
cord_y=cord_y+110;
cont=1;
}
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url objectAtIndex:i]]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cord_x, cord_y, 100, 100)];
[imageView setImage:image];
[self.view addSubview:imageView];
cord_x=cord_x+110;
}

animationArray only playing last image

I currently have this method buttonHit that calls playAnimationToNumber which accepts an int, which is then in turn used to run through a loop playing an array of images for each iteration, however it only seems to be playing the last array.. can you see why? because I am slightly lost and would appreciate the help.
//attached to button and it calls animation method.
- (void)buttonHit:(id)sender{
[self playAnimationToNumber:5];
}
- (void)playAnimationToNumber:(int)number{
for (int counter=1; counter<=number; counter++) {
NSString *imageNameForFirstNumber = [NSString stringWithFormat:#"Flap%i.png", counter];
NSArray *imagesForAnimation = [NSArray arrayWithObjects:[UIImage imageNamed:#"FlapMoving1.png"], [UIImage imageNamed:#"FlapMoving2.png"], [UIImage imageNamed:imageNameForFirstNumber], nil];
animationArray.animationImages = [[NSArray alloc] initWithArray:imagesForAnimation];
animationArray.animationDuration = 0.5;
animationArray.animationRepeatCount = 1;
[animationArray startAnimating];
[self.view addSubview:animationArray];
}
[animationArray release];
}
WORKING CODE
'
- (void)playAnimationToNumber:(int)number{
NSMutableArray *imagesForAnimation = [[NSMutableArray alloc] initWithCapacity:0];
for (int counter=1; counter<=number; counter++) {
NSString *imageNameForFirstNumber = [NSString stringWithFormat:#"Flap%i.png", counter];
[imagesForAnimation addObject:[UIImage imageNamed:#"FlapMoving1.png"]];
[imagesForAnimation addObject:[UIImage imageNamed:#"FlapMoving2.png"]];
[imagesForAnimation addObject:[UIImage imageNamed:imageNameForFirstNumber]];
}
animationArray.animationImages = [NSArray arrayWithArray:imagesForAnimation];
animationArray.animationDuration = 5.9;
animationArray.animationRepeatCount = 1;
[animationArray startAnimating];
[self.view addSubview:animationArray];
[imagesForAnimation release];
}'
Here you go try this code.
- (void)playAnimationToNumber:(int)number{
NSMutableArray *imagesForAnimation = [[[NSMutable alloc] initWithCapacity:0] autoRelease];
[imagesForAnimation addObject:[UIImage imageNamed:#"FlapMoving1.png"]];
[imagesForAnimation addObject:[UIImage imageNamed:#"FlapMoving2.png"]];
for (int counter=1; counter<=number; counter++) {
NSString *imageNameForFirstNumber = [NSString stringWithFormat:#"Flap%i.png", counter];
[imagesForAnimation addObject:[UIImage imageNamed:imageNameForFirstNumber]];
}
animationArray.animationImages = [NSArray arrayWithArray:imagesForAnimation];
animationArray.animationDuration = 0.5;
animationArray.animationRepeatCount = 1;
[animationArray startAnimating];
[self.view addSubview:animationArray];
}
}

OpenFlow with XML Object

Trying to use OpenFlow API for iPhone SDK.
I need the openflow to load pictures from a (image) link.
This is how it always works with local images:
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
And I tried this:
- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index {
NSString *photoUrl = [[oldEntries objectAtIndex: 0] objectForKey: #"still"];
NSURL *photomake = [NSURL URLWithString:photoUrl];
NSLog(#"theConnection: %#",photoUrl);
NSLog(#"theConnection: %#",photomake);
AFGetImageOperation *getImageOperation = [AFGetImageOperation alloc];
// We're getting our images from the Flickr API.
getImageOperation.imageURL = photomake;
[loadImagesOperationQueue addOperation:getImageOperation];
[(AFOpenFlowView *)self.view setNumberOfImages:1];
[getImageOperation release];
}
But it just won't work. Can anyone help me?
Without knowing what 'won't work' it's hard to say, please clarify and supply the actual URL you are creating.
The only line that looks suspect is this one:
AFGetImageOperation *getImageOperation = [AFGetImageOperation alloc];
Should be:
AFGetImageOperation *getImageOperation = [[AFGetImageOperation alloc] init];
It's much easier to use:
NSURL *imageUrl = [[NSURL alloc] initWithString:[#"path/to/the/file.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:imageData];
put all the images in an Array
and then you call you first function
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:**image** forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];

AFOpenFlow touchevent Image

does anybody know a solution that i could implement at this code,
that the pictures could be touched and an event will be started?
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
I hope someone could help me and
sorry for my bad english :)
in AFOpenFlowView.m methods touchesBegan, touchMoves and touchEnded are already overriden, so just create a new delegate method for your usage and adapt these methods.

AutoResizing Image no imageView

i am using AFOpenFlow in my app, but now the Problem is,
the pictures I use are not the same size because they are from the web!
And how can i autoResizing the image at the view of the iPhone???
Here is my code from this method:
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
Thank you beforehand
Marco
scale your image before set to open flow
change your code in the loop as below:
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
UIImage *oriImage = [UIImage imageNamed:imageName];
[imageName release];
UIImage *requiredImage = [UIImage imageWithCGImage:[oriImage CGImage] scale:768/520.0 orientation:UIImageOrientationUp];
[(AFOpenFlowView *)self.view setImage:requiredImage forIndex:i];
NSLog(#"%d is the index",i);