how to stop the animation at the last frame? - iphone

How I can stop animation on the last frame.
Code here:
- (void)method{
NSMutableArray *images = [[NSMutableArray alloc]init];
NSInteger i;
for (i = 0; i < 75; i++){
NSString *str = [NSString stringWithFormat:#"pictures%i.png", i];
UIImage *img =[UIImage imageNamed:str];
[images addObject:img];
}
// Begin the animation
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
//somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];
//[somePicture isAnimating]==0;
somePicture.frame = CGRectMake(0, 0, 320, 480);
[somePicture setAnimationImages:images];
somePicture.animationDuration = 5.0f;
somePicture.animationRepeatCount = 1;
[somePicture startAnimating];
somePicture.center = CGPointMake(160.0f, 230.0f);
//[images release];
[NSTimer scheduledTimerWithTimeInterval: 0.5f target: self selector: #selector(tick) userInfo: nil repeats: YES];
[self.view addSubview:somePicture];
}
-(void)tick{
if(![somePicture isAnimating]) { [timer invalidate]; [somePicture stopAnimating]; timer=nil; NSLog(#"#ye1111eah!!!");}
else
NSLog(#"#y");}
I know that I must used NSTimer but i don't know how :(

Why do you allocate somePicture twice
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];
Can't you just use this
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
somePicture.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
UPDATE
I think this will do it
- (void)method{
NSMutableArray *images = [[NSMutableArray alloc]init];
NSInteger i;
for (i = 0; i < 75; i++){
NSString *str = [NSString stringWithFormat:#"pictures%i.png", i];
UIImage *img =[UIImage imageNamed:str];
[images addObject:img];
}
// Begin the animation
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"8Ball_003_00075.png"]];
somePicture.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
self.view addSubview:somePicture];
//[somePicture isAnimating]==0;
[somePicture setAnimationImages:images];
somePicture.animationDuration = 5.0f;
somePicture.animationRepeatCount = 1;
somePicture.center = CGPointMake(160.0f, 230.0f);
[somePicture startAnimating];
[self performSelector:#selector(tick) withObject:self afterDelay:5.0];
}
- (void)tick
{
[somePicture stopAnimating];
if(![somePicture isAnimating])
{
NSLog(#"#ye1111eah!!!");
}
else
NSLog(#"#y");
}
hope this helps

Related

Animate array of image and textview together in UIView

I want to aniate array of imageview and textview together in uiview.
Right now i am animating image view separate and textview separate which is not looking that good.
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
baseView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:252.0/255.0 blue:199.0/255.0 alpha:1.0];
[self.view addSubview:baseView];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 30, 200, 200)];
self.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
nil];
// all frames will execute in 25 seconds
self.imageView.animationDuration = 20;
[self.imageView startAnimating];
[baseView addSubview:self.imageView];
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 250, 300, 200)];
self.textView.textColor = [UIColor brownColor];
myArray = [[NSMutableArray alloc] initWithObjects:#"string1", #"string2", #"string3", #"string4", #"string5", ...., nil];
[NSTimer scheduledTimerWithTimeInterval:2.5
target:self
selector:#selector(updateText:)
userInfo:nil
repeats:YES];
[baseView addSubview: self.textView];
- (void)updateText:(NSTimer *)theTimer
{
if (index < [myArray count])
{
self.textView.text = [self.myArray objectAtIndex:index];
index++;
}
else
index = 0;
[timer invalidate];
}
How i can animate both image view and textview together within UIView.
Thanks for help.
How about this,,
in your .h
NSArray *imgsArray;
NSArray *myArray;
int indx;
NSTimer *timer;
and start your animation
-(void)start{
imgsArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],nil];
myArray = [[NSMutableArray alloc] initWithObjects:#"string1", #"string2", #"string3", #"string4", #"string5", nil];
timer = [NSTimer scheduledTimerWithTimeInterval:2.5
target:self
selector:#selector(updateText:)
userInfo:nil
repeats:YES];
}
- (void)updateText:(NSTimer *)theTimer
{
if (indx < myArray.count)
{
self.textView.text = [self.myArray objectAtIndex:indx];
self.imageView.image = [self.imgsArray objectAtIndex:indx];
indx++;
}
else
indx = 0;
}

Array of Images display in UIScrollview and Paging successfully but cannot properly zoom-in images in scrollview

I am working on UIScrollview with ImageArray. Scrolling and paging are working but I not able to zoom each image. My code of image Scrollview is below :-
#define IMAGE_WIDTH 320
#define IMAGE_HEIGHT 360
- (void)viewDidLoad
{
[super viewDidLoad];
// TODO – fill with your photos
NSArray *photos = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1m.jpg"],
[UIImage imageNamed:#"photo2m.jpg"],
[UIImage imageNamed:#"photo3m.jpg"],
[UIImage imageNamed:#"photo4m.jpg"],
nil] retain];
// note that the view contains a UIScrollView in aScrollView
int i=0;
for ( NSString *image in photos )
{
UIImage *images = [photos objectAtIndex:i];
imageView = [[UIImageView alloc] initWithImage:images];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.frame = CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
[self.scrollViewimages addSubview:imageView];
[imageView release];
}
self.scrollViewimages.contentSize = CGSizeMake(IMAGE_WIDTH*i, IMAGE_HEIGHT);
self.scrollViewimages.delegate = self;
}
Need to help for implementing pinch zoom of every images. Please help !
for ( NSString *image in photos )
{
UIImage *images = [photos objectAtIndex:i];
imageView = [[UIImageView alloc] initWithImage:images];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.tag = 1;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
scrollView.delegate = self;
scrollView.maximumZoomScale = 3.0f
imageView.frame = scrollView.bounds;
[scrollView addSubview:imageView];
[imageView release];
[self.scrollViewimages addSubview:scrollView];
}
And Implement the delegate method in UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:1];
}
#define IMAGE_FOR_ZOOM_TAG (1)
Call following custom method
-(void) setupProductImageViewContainerUI
{
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
if(self.dimView)
{
for(UIView *subView in self.dimView.subviews)
[subView removeFromSuperview];
[self.dimView removeFromSuperview]; self.dimView = nil;
}
self.dimView = [[UIView alloc] initWithFrame:window.bounds];
self.dimView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
self.dimView.userInteractionEnabled = YES;
[window addSubview:self.dimView];
self.imgContainerView = [[UIView alloc] init];
self.imgContainerView.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
self.imgContainerView.backgroundColor = [UIColor whiteColor];
[self.dimView addSubview:self.imgContainerView];
UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:window.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.delegate = self;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;
[self.imgContainerView addSubview:mainScrollView];
CGRect innerScrollFrame = mainScrollView.bounds;
IndexOfSlidingPhoto = 0;
for(int i = 0 ; i < listOfImages.count; i++)
{
UIImage *imgProduct = [GeneralClass getImageForSreenFromScreenTable:#"" orCustomTable:#"" OrDefaultImga:[listOfImages objectAtIndex:i]];
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:imgProduct];
imageForZooming.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
imageForZooming.tag = IMAGE_FOR_ZOOM_TAG;
imageForZooming.contentMode = UIViewContentModeScaleAspectFit;
UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 1.0f;
pageScrollView.maximumZoomScale = 20;
pageScrollView.zoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView addSubview:imageForZooming];
[mainScrollView addSubview:pageScrollView];
if (i < listOfImages.count -1)
innerScrollFrame.origin.x += innerScrollFrame.size.width;
mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);
[window bringSubviewToFront:self.dimView];
}
#pragma Mark -
#pragma Mark - UIScrollView Delegate Methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView viewWithTag:IMAGE_FOR_ZOOM_TAG];
}

How to Save only that image which is long pressed and selected by user

Currently it is saving all images but i want to save only the one which is long pressed not all images in one shot only the one user selects
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
//[myButton addGestureRecognizer:tap];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
_imageView.tag = 128;
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
//imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
//[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
}
How to code for that
Thanks for help
In the handleLongPress: function, use this:
UIGestureRecognizer *gestureRecognizer;
CGPoint gesturePoint = [gestureRecognizer locationInView:self.view];
Then check all of your buttons using a for loop like you have there. Use this to check if it is the pressed image:
if(CGRectContainsPoint(myButton.frame,gesturePoint)){
//save the image stored in myButton
//break will exit the for loop early, so if the view comes early in the list,
//it doesn't have to check the rest.
break;
}

Thumbnail grid of images

How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.
this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed

UIImage imageWithData

I have a UITable and when I click on a row I load a set of images.
I create the images with
UIImage *image = [UIImage imageWithData:data];
The problem is that ever time I click on the same row, the images are re-allocated into memory, like if no cache it is used and no memory is freed, so the allocated memory keep increasing. (I see this using the Inspector on both the device and the emulator)
I am releasing everything and also, with clang, everything looks fine.
The point is that after a while my app crashes.
Any suggestion or idea?
EDIT:
Here is the portion of code I think is generating the issue:
UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];
for (int i=0; i<[contentArray count]; i++) {
float targetWidth = rectScrollView.size.width;
float targetHeight = rectScrollView.size.height;
Link *tmp_link = [contentArray objectAtIndex:i];
AsyncImageView* asyncImage = nil;
if(tmp_link!=nil){
CGRect imageFrame = CGRectMake(targetWidth*i, 0, targetWidth, targetHeight);
//asyncImage = [[[AsyncImageView alloc] initWithFrame:imageFrame ] autorelease];
asyncImage = [[AsyncImageView alloc] initWithFrame:imageFrame ];
asyncImage.contentMode = UIViewContentModeScaleAspectFit;
NSURL* url = [NSURL URLWithString:[[IMG_SERVER_URL stringByAppendingString: tmp_link.url] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
[asyncImage loadImageFromURL:url];
[self.scrollView addSubview:(UIImageView *)asyncImage];
[asyncImage release];
}
if ([tmp_link.caption length] > 0 && asyncImage!=nil) {
float marginTop = 25;
UILabel *caption = [[UILabel alloc] init];
caption.text = tmp_link.caption;
caption.textAlignment = UITextAlignmentCenter;
caption.textColor = [UIColor whiteColor];
caption.font = [UIFont systemFontOfSize:12];
caption.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.4];
CGRect captionFrame = CGRectMake((i * targetWidth), marginTop+10, targetWidth, 20.0f);
caption.frame = captionFrame;
[self.scrollView addSubview:caption];
[caption release];
}
//[asyncImage release];
}
[main addSubview:scrollView];
[scrollView release];
if (pageControlEnabledTop) {
rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
}
else if (pageControlEnabledBottom) {
//rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
rectPageControl = CGRectMake(0, (scrollHeight), scrollWidth, 15);
}
if (pageControlEnabledTop || pageControlEnabledBottom) {
//pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
pageControl = [[UIPageControl alloc] initWithFrame:rectPageControl];
pageControl.numberOfPages = [contentArray count];
pageControl.currentPage = page;
[main addSubview:pageControl];
[pageControl release];
}
if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
//if (margin) [margin release];
return (UIScrollView *)main;
And it is used like this:
reviewImagesImage = [[IGUIScrollViewImage alloc] init];
if([currentReview.linkScreenshots count]>0){
//reviewImages
reviewImages = [[UIView alloc] init];
//placing the images
CGRect frameImages = reviewImages.frame;
frameImages.origin.x = 10;
frameImages.origin.y = (infoView.frame.origin.y + infoView.frame.size.height + 10.0f);
frameImages.size.width = 300;
frameImages.size.height = 350.0f;
reviewImages.frame = frameImages;
reviewImages.backgroundColor = [UIColor clearColor];
[reviewImagesImage setContentArray:currentReview.linkScreenshots];
[reviewImagesImage setWidth:reviewImages.frame.size.width andHeight: reviewImages.frame.size.height];
[reviewImagesImage enablePageControlOnBottom];
[reviewImagesImage setBackGroudColor: [UIColor clearColor]];
UIScrollView *tmp_sv = [reviewImagesImage getWithPosition:0];
[reviewImages addSubview:tmp_sv];
[self.view addSubview:reviewImages];
[reviewImages release];
}
I have noticed that if I try to release reviewImagesImage my app crashes :/
Of course no cache is used because new UIImage will be created from the data every time. If memory usage significantly increased like you have said, it is most likely that it is our fault that unintentionally keep the reference retained somewhere.
Once you've set your image with imageWithData, save the image to the filepath in the app bundle.