generating animation in different spots in view randomly - objective-c

Hello I am new to animations and just started playing around with it recently. I want to have a circle (ring) displayed that goes from a small ring to a large ring, dissapears and then does the process all over again except in a different location.
To do this I have created 6 different image sizes of the same ring. The following code is how I have done it but I do not know how to repeat the process (AkA it runs the method but the location of the ring stays the same, I need to loop the random number generator but don't know when to do a check if the image has stopped). If this is not the right way to go about this, please let me know! P.S. The ring looks very ugly by the time it gets to its largest stage, not sure why.
Thanks
-(void) doWork
{
NSInteger number;
NSInteger number2;
NSLog (#"here");
number = (arc4random()%100)+1;
number2 = (arc4random()%100)+1;
NSLog (#" hello numer1: %d", number);
NSLog (#" hello numer2: %d", number2);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(number, number2, 135, 135)];
imageView.animationImages= [NSArray arrayWithObjects:
[UIImage imageNamed:#"smallestRing.png"],
[UIImage imageNamed:#"smallRing.png"],
[UIImage imageNamed:#"mediumRing.png"],
[UIImage imageNamed:#"large2Ring.png"],
[UIImage imageNamed:#"largeRing.png"], nil];
imageView.animationDuration= 2.5;
imageView.animationRepeatCount= 0;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView startAnimating];
[self.view addSubview:imageView];
/*[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(doWork) userInfo:nil repeats:YES];*/
}

if resizing (or other transforming such as rotation or transition) is the only animation you need, you could use method +animateWithDuration (you can found description here)
it will look like:
-(void) doWork
{
NSInteger number;
NSInteger number2;
NSLog (#"here");
number = (arc4random()%100)+1;
number2 = (arc4random()%100)+1;
NSLog (#" hello numer1: %d", number);
NSLog (#" hello numer2: %d", number2);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(number, number2, 135, 135)];
imageView.image = [UIImage imageNamed:#"largeRing.png"];
[self.view addSubview:imageView];
[self animate:imageView enlarge:YES];
}
-(void) animate:(UIImageView*)imageView enlarge:(BOOL)flag
{
if (flag)
{
// then animating is starting
// and imageView will appear in random point of view
int randomX = arc4random()%self.view.frame.size.width;
int randomY = arc4random()%self.view.frame.size.height;
imageView.center = CGPointMake(randomX, randomY);
}
double scale = flag ? 2 : 0.5;
[UIView animateWithDuration:5
animations:^{ imageView.transform = CGAffineTransformMakeScale(scale,scale); }
completion:^(BOOL finished){ [self animate:imageView: enlarge:!flag]; }];
}

You can query the bool isAnimating in order to determine if the animation of the UIImageView ended
P.S:
Probably it looks ugly because of
imageView.contentMode = UIViewContentModeScaleAspectFit;
Try something like
imageView.contentMode = UIViewContentModeCenter;

Related

How control memory usage when applying CIFilters?

When I apply CIFilters to images the memory usage keeps growing and I don't know what to do.
I've tried everything I could:
using #autoreleasepool:
- (UIImage *)applySepiaToneTo:(UIImage *)img //Sepia
{
#autoreleasepool
{
CIImage *ciimageToFilter = [CIImage imageWithCGImage:img.CGImage];
CIFilter *sepia = [CIFilter filterWithName:#"CISepiaTone"
keysAndValues: kCIInputImageKey, ciimageToFilter,
#"inputIntensity", #1.0, nil];
return [self retrieveFilteredImageWithFilter:sepia];
}
}
- (UIImage *)retrieveFilteredImageWithFilter:(CIFilter *)filtro
{
#autoreleasepool
{
CIImage *ciimageFiltered = [filtro outputImage];
CGImageRef cgimg = [_context createCGImage:ciimageFiltered
fromRect:[ciimageFiltered extent]];
UIImage *filteredImage = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
return filteredImage;
}
}
I'm also downsizing the image to be filtered and doing the filtering in a background thread:
- (void)filterWasSelected:(NSNotification *)notification
{
self.darkeningView.alpha = 0.5;
self.darkeningView.userInteractionEnabled = YES;
[self.view bringSubviewToFront:self.darkeningView];
[self.activityIndic startAnimating];
[self.view bringSubviewToFront:self.activityIndic];
int indice = [notification.object intValue];
__block NSArray *returnObj;
__block UIImage *auxUiimage;
if(choosenImage.size.width == 1280 || choosenImage.size.height == 1280)
{
UIImageView *iv;
if(choosenImage.size.width >= choosenImage.size.height)
{
float altura = (320 * choosenImage.size.height)/choosenImage.size.width;
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,altura)];
iv.image = choosenImage;
}
else
{
float largura = (choosenImage.size.width * 320)/choosenImage.size.height;
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,largura,320)];
iv.image = choosenImage;
}
UIGraphicsBeginImageContextWithOptions(iv.bounds.size, YES, 0.0);
[iv.layer renderInContext:UIGraphicsGetCurrentContext()];
auxUiimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else
auxUiimage = choosenImage;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
if(artisticCollection)
returnObj = [self.filterCoordinator setupFilterArtisticType:indice toImage:auxUiimage];
else
returnObj = [self.filterCoordinator setupFilterOldOrVintageType:indice toImage:auxUiimage];
dispatch_async(dispatch_get_main_queue(), ^{
self.darkeningView.alpha = 0.3;
self.darkeningView.userInteractionEnabled = NO;
[self.activityIndic stopAnimating];
[self.view bringSubviewToFront:stageBackground];
[self.view bringSubviewToFront:stage];
[self.view bringSubviewToFront:self.filtersContainerView];
[self.view bringSubviewToFront:self.framesContainerView];
[self.view bringSubviewToFront:self.colorsContainerView];
if(returnObj)
{
auxUiimage = [returnObj firstObject];
NSLog(#"filtered image width = %f and height = %f", auxUiimage.size.width, auxUiimage.size.height);
returnObj = nil;
choosenImageContainer.image = auxUiimage;
}
});
});
}
I've also tried creating the context using the contextWithEAGLContext method, nothing changed.
I've researched a lot including stack overflow and found nothing.
Until I place the image in the image view (the image comes from the photo album) I'm only using 23 mega of memory, when I apply a filter, the use jumps to 51 mega and does not comes down. If I continue to apply other filters the memory usage only grows.
There's no linking in my app, I've checked in Instruments.
Also the bringSubviewToFront methods are not responsible, I've checked.
It's in the creation of the CIImage followed by the creation of the CIFilter object.
I know that in the process of applying the filter data is loaded into memory, but how to clean the memory after applying the filter?
Is there any secret that I'm not aware of?? Please help

NSMutableArray not holding UIImages properly?

I have enabled my Cocoa Touch app to be navigable by swiping left or right to alter positions in history. The animation is kind of done like Android's "card" style. Where swiping to the left (<--) just moves the current screen image out of the way, while showing the previous view beneath it.
This works fine, but when I want to swipe the other way (-->), to go back, I need to get the previous image, and move that over the current view. Now, I had this working if I only store the previous image, but what if I go <-- a few times, then I will not have enough images.
So, the solution is obvious, use an NSMutableArray and just throw the latest image at the front of the array, and when you swipe the other way, just use the first image in the array. However, the image never shows when I start the animation. It just shows nothing. Here's the required methods you should see:
-(void)animateBack {
CGRect screenRect = [self.view bounds];
UIImage *screen = [self captureScreen];
[imageArray insertObject:screen atIndex:0]; //Insert the screenshot at the first index
imgView = [[UIImageView alloc] initWithFrame:screenRect];
imgView.image = screen;
[imgView setHidden:NO];
NSLog(#"Center of imgView is x = %f, y = %f", imgView.center.x, imgView.center.y);
CGFloat startPointX = imgView.center.x;
CGFloat width = screenRect.size.width;
NSLog(#"Width = %f", width);
imgView.center = CGPointMake(imgView.center.x, imgView.center.y);
[self.view addSubview: imgView];
[self navigateBackInHistory];
[UIView animateWithDuration:.3 animations:^{
isSwiping = 1;
imgView.center = CGPointMake(startPointX - width, imgView.center.y);
} completion:^(BOOL finished){
// Your animation is finished
[self clearImage];
isSwiping = 0;
}];
}
-(void)animateForward {
CGRect screenRect = [self.view bounds];
//UIImage *screen = [self captureScreen];
UIImage *screen = [imageArray objectAtIndex:0]; //Get the latest image
imgView = [[UIImageView alloc] initWithFrame:screenRect];
imgView.image = screen;
[imgView setHidden:NO];
NSLog(#"Center of imgView is x = %f, y = %f", imgView.center.x, imgView.center.y);
CGFloat startPointX = imgView.center.x;
CGFloat width = screenRect.size.width;
NSLog(#"Width = %f", width);
imgView.center = CGPointMake(imgView.center.x - width, imgView.center.y);
[self.view addSubview: imgView];
[UIView animateWithDuration:.3 animations:^{
isSwiping = 1;
imgView.center = CGPointMake(startPointX, imgView.center.y);
} completion:^(BOOL finished){
// Your animation is finished
[self navigateForwardInHistory];
[self clearImage];
isSwiping = 0;
}];
}
-(void)clearImage {
[imgView setHidden:YES];
imgView.image = nil;
}
-(void)navigateBackInHistory {
[self saveItems:self];
[self alterIndexBack];
item = [[[LEItemStore sharedStore] allItems] objectAtIndex:currentHistoryIndex];
[self loadItems:self];
}
-(void)navigateForwardInHistory {
[imageArray removeObjectAtIndex:0]; //Remove the latest image, since we just finished swiping this way.
[self saveItems:self];
[self alterIndexForward];
item = [[[LEItemStore sharedStore] allItems] objectAtIndex:currentHistoryIndex];
[self loadItems:self];
}
Note that imgView is a class-level UIImageView and imageArray is a class level array.
Any ideas? Thanks.
Edit
Here's the code at the top of my .m to initalize it. Still does not work.
.....
NSMutableArray *imageArray;
- (void)viewDidLoad
{
[super viewDidLoad];
imageArray = [imageArray initWithObjects:nil];
It looks like you forgot to create the array. Something like this at the appropriate time would do (assuming ARC):
imageArray = [NSMutableArray array];
Glad that worked out.

Removing UIImageView and getting the memory back (my memory is leaking)

I am making a simple drawing application, but a have problems with memory. After a few minutes using it, it just collapse, it makes the iPad very slow.
The lines the user create drawing are made of a UIImageView of 2x2 pixel, so it's this UIImageView is created several times:
UIImageView*dibujo = [[UIImageView alloc] initWithFrame:CGRectMake(x-1,y-1,2,2)];
dibujo.image = [UIImage imageNamed:#"pixelde2po2.png"];
tagdedibujo=tagdedibujo+1;
dibujo.tag=tagdedibujo;
[self.view addSubview:dibujo];
And it's being erased in this way:
for (int aa=ultimotag+1; aa<=tagdedibujo; aa++) {
//NSLog(#"destruyendo: %i", aa);
UIImageView*dibujo1 = (UIImageView *)[self.view viewWithTag:aa];
[UIView animateWithDuration:0.5 animations:^{
dibujo1.alpha=0;
}];
}
[self performSelector:#selector(matardibujo) withObject:(self) afterDelay:(0.51)];
ultimotag=tagdedibujo;
-(void) matardibujo{
for (int aa=ultimotag+1; aa<=tagdedibujo; aa++) {
[[self.view viewWithTag:aa] removeFromSuperview];
}
}
Even after erasing de draw (matardibujo) i don't get the memory back. Even it grows. So i think the erasing part is the one that's messing here.
I would preciate your help, i'm kind of new in this. Thanks :).
Could you replace dibujo.image = [UIImage imageNamed:#"pixelde2po2.png"]; with the following line of code to see if it resolves your issue?
NSString *fileName = [[NSBundle mainBundle] pathForResource:#"pixelde2po2" ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];

UIScrollView with UIImageview and gestures using transitionFromView acting strange

I made a view which holds a UIScrollview:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 65, 300, 188)];
//BackViews will hold the Back Image
BackViews = [[NSMutableArray alloc] init];
for (int i=0; i<BigPictures.count; i++) {
[BackViews addObject:[NSNull null]];
}
FrontViews = [[NSMutableArray alloc] initWithCapacity:BigPictures.count];
[self.pageControl setNumberOfPages:BigPictures.count];
Then I add several UIImageviews containing images:
//BigPictures holds objects of type UIImage
for (int i = 0; i < BigPictures.count; i++) {
UIImageView *ImageView = [[UIImageView alloc] initWithImage:[BigPictures objectAtIndex:i]];
ImageView.frame = [self.scrollView bounds];
[ImageView setFrame:CGRectMake(self.scrollView.frame.size.width * i, ImageView.frame.origin.y, ImageView.frame.size.width, ImageView.frame.size.height)];
//this saves the FrontView for later (flip)
[FrontViews insertObject:ImageView atIndex:i];
[self.scrollView addSubview:test];
}
// Detect Single Taps on ScrollView
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(flip)];
[self.scrollView addGestureRecognizer:tap];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
Ok so far so good. Now the method which does the flipImage part:
- (void)flip {
int currentPage = self.pageControl.currentPage;
UIView *Back = nil;
if ([BackViews objectAtIndex:currentPage] == [NSNull null]) {
//CreateBackView is just creating an UIView with text in it.
Back = [self CreateBackView];
[BackViews replaceObjectAtIndex:currentPage withObject:Back];
[UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[BackViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
} else {
[UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[FrontViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromRight completion:NULL];
[BackViews replaceObjectAtIndex:currentPage withObject:[NSNull null]];
}
[self.view addSubview:Back];
[self rebuildScrollView];
}
This is what rebuildScrollView does:
- (void)rebuildScrollView
{
for (UIView *subview in self.scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 0; i < BigPictures.count; i++) {
if ([BackViews objectAtIndex:i] == [NSNull null]) {
[self.scrollView addSubview:[FrontViews objectAtIndex:i]];
} else {
[self.scrollView addSubview:[BackViews objectAtIndex:i]];
}
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
}
So the behavior is the following:
If I click on the first image (1 of 3 in scrollview) the effect is the way I want it, meaning the frontimage turns around and shows the empty (white) back with some text in the middle
If I click on the second image, the image turns but the back is completely empty showing the grey background of the window. If I scroll to the other images, the still show the front image (as expected)
Now I click on the third image and its the same as 1) great.
Current layout is now [BackView, Nothing, Backview)
Lets run that again. But now I click on the last image and its the same as 2) :(
Any ideas whats going wrong here?
EDIT: Some new findings. I doubled the amount of pictures and this is how Front and Backviews are placed (after flipping each one). P = Picture & B = Backview.
P_1(B_1) - actually the only correct one
P_2(empty - should be B_2)
P_3(B_2 - should be B_3)
P_4(empty - should be B_4)
P_5(B_3 - should be B_5)
P_6(empty - should be B_6)
Did a complete rebuild and now it works. Really strange because I used the exact same code.

selecting specific images in a UIScrollview

i have a scrollview which has images added as subviews. i need to be able to select or tap on the images. in other words capture the name of the image tapped. please see my code below.
for (j = 1; j <= 12; j++)
{
NSString *imageName = [NSString stringWithFormat:#"%#",[months objectAtIndex:j-1]];
NSLog(#"imagename is %#",imageName);
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = smallScrollObjHeight;
rect.size.width = smallScrollObjWidth;
imageView.frame = rect;
imageView.tag = j;
[smalltapRecognizer setNumberOfTapsRequired:1];
[smalltapRecognizer setDelegate:self];
[imageView addGestureRecognizer:smalltapRecognizer];
[smalltapRecognizer release];*/
NSLog(#"tag value is %d ",imageView.tag);
[monthscroll addSubview:imageView];
[imageView release];
}
[self layoutsmallScrollImages];
UITapGestureRecognizer *smalltapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(smalltapped:)];
[smalltapRecognizer setNumberOfTapsRequired:1];
[smalltapRecognizer setDelegate:self];
[monthscroll addGestureRecognizer:smalltapRecognizer];
[smalltapRecognizer release];
//----------------------------
-(void)smalltapped:(id)sender
{
NSLog(#"tapped");
//Need to know how to proceed from here.
}
my program detects the tap and prints the tap log in the smalltapped function. i need to know how can i capture the name of the images selected or tapped on.
Why don't you just use UIButtons in your scrollView instead? That way you can use the standard UIButton methods, don't need to bother with gesture recognizers, and you'll be able to see the image on the button being pressed.
Associate a unique tag value with each UIImageView instance:
In viewDidLoad or wherever your images are initialised:
myImageView1.tag = 1;
myImageView2.tag = 2;
myImageView3.tag = 3;
then try a gesture or something like:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
int t = [touch view].tag;
UIImageView *imageView = (UIImageView *) [self.view viewWithTag:t];
//your logic here since you have recognized the imageview on which user touched
}
haven't tried something like this before but it should probably work.
Edit:I copy pasted this code from my answer on another similar question here
So adjust the conditions according to your requirements please.