SDWebImage Class - iphone

How can I use this method to perform activity indicator in placeholder ?
- (void)createActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle) activityStyle {
if ([self activityIndicator] == nil) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
self.activityIndicator.autoresizingMask = UIViewAutoresizingNone;
//calculate the correct position
float width = self.activityIndicator.frame.size.width;
float height = self.activityIndicator.frame.size.height;
float x = (self.frame.size.width / 2.0) - width/2;
float y = (self.frame.size.height / 2.0) - height/2;
self.activityIndicator.frame = CGRectMake(x, y, width, height);
self.activityIndicator.hidesWhenStopped = YES;
[self addSubview:self.activityIndicator];
}
[self.activityIndicator startAnimating];
}
I appreciate any help

You can do it this way, i use it in this example for updating cells, but it is the same for any use you need.
What i'm doing is setting the UIImageView alpha to 0,Adding a UIActivityIndicatore,and calling a block to get notified when the image has bees loaded.
When its loaded, the UIImageView alpha will be set back to 1, and remove the UIActivityIndicator.
-(void)updateCell {
self.imageView = [[UIImageView alloc]initWithFrame:frameOfCell];
self.imageView.alpha = 0;
[self addSubview:self.imageView];
//Activity indivator:
self.activity= [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activity.hidesWhenStopped = YES;
self.activity.frame = CGRectMake((frameOfCell.size.width/2)-10, (frameOfCell.size.height/2)-20, 40, 40);
[self.activity startAnimating];
[self addSubview:self.activity];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
NSURL *urlOfImage = [NSURL URLWithString:self.imageName];
self.imageView.alpha = 0;
[self.imageView setImageWithURL:urlOfImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
///
self.imageView.alpha = 1;
//Removing activity indicator:
[self.activity stopAnimating];
[self.activity removeFromSuperview];
}];
}
Hope this helps

Here is a Category on UIImageView, adding a progress view while images are downloaded using SDWebImage.
What you should to do is to change UIProgressView with UIActivityIndicatorView
Try to modify code like this:
- (void)removeIndicatorView{
[self.activityIndicator stopAnimating];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock withActivityStyle:(UIActivityIndicatorViewStyle) activityStyle {
[self createActivityIndicatorWithStyle:activityStyle];
__weak typeof(self) weakSelf = self;
[self setImageWithURL:url
placeholderImage:placeholder
options:options
progress:^(NSUInteger receivedSize, long long expectedSize) {
//If you don't care progress, do nothing
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[weakSelf removeIndicatorView];
if (completedBlock) {
completedBlock(image, error, cacheType);
}
}
];
}

Related

Contents are shuffles in GridView

I am new to IOS development. I am using custom GridView to load the content in my project, and it is working fine for Simulator when the content reloads. But in iPad the content gets shuffles when reloads.
- (void)reloadData {
if (_gridViewDelegate && self.superview != nil) {
CGFloat maxX = 0.f;
CGFloat maxY = 0.f;
_scrollView.pagingEnabled = YES;
self.gridRects = [self.gridViewDelegate rectsForCellsInGridView:self];
[self.gridCells.allValues makeObjectsPerformSelector:#selector(removeFromSuperview)];
[self.reuseableCells addObjectsFromArray:self.gridCells.allValues];
[self.gridCells removeAllObjects];
for (NSValue *rectValue in self.gridRects) {
CGRect rect = [rectValue CGRectValue];
maxX = MAX(maxX, rect.origin.x + rect.size.width);
maxY = MAX(maxY, rect.origin.y + rect.size.height);
}
CGFloat pageWidth = self.scrollView.frame.size.width;
self.maximumContentWidth=maxX;
maxX = MAX(MIN(maxX, self.maximumContentWidth), self.contentSize.width);
maxY = MAX(MIN(maxX, self.maximumContentHeight), self.contentSize.height);
maxX=pageCount*pageWidth;
self.scrollView.contentSize = CGSizeMake(maxX, maxY);
[self loadCellsInRect:self.visibleRect];
[self updateStickyViewsPosition];
[pageNumberLable removeFromSuperview];
currentPage = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if(pageWidth == 1024)
{
pageNumberLable=[[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.contentOffset.x+990, 690, 20, 20)];
pageNumberLable.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
else
{
pageNumberLable=[[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.contentOffset.x+740, 943, 20, 20)];
pageNumberLable.backgroundColor=[UIColor redColor];
pageNumberLable.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
pageNumberLable.text=[NSString stringWithFormat:#"%d",currentPage+1];
pageNumberLable.textColor=[UIColor blackColor];
pageNumberLable.font = [UIFont boldSystemFontOfSize:16];
pageNumberLable.backgroundColor=[UIColor clearColor];
[self.scrollView addSubview:pageNumberLable];
pageIndex = [NSUserDefaults standardUserDefaults];
[pageIndex setInteger:currentPage+1 forKey:#"integerKey"];
}
}
-(void)reloadGrid
{
if(rotate == newIntValue)
{
[spinningLibrary stopAnimating];
[update removeFromSuperview];
settingLabel.hidden=YES;
spinningLibrary.hidden=YES;
}
if(rotate<newIntValue)
{
rotate++;
[self.gridView scrollRectToVisible:CGRectMake(768*rotate, 0, self.gridView.frame.size.width, self.gridView.frame.size.height) animated:YES];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(reloadGrid123) userInfo:nil repeats:NO];
}
[NSThread sleepForTimeInterval:0.2];
self.gridView.scrollView.hidden=NO;
[settingsPopoverController dismissPopoverAnimated:YES];
}
You're comparing iPad's RAM and your machine's RAM.It's not fair though.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(reloadGrid123) userInfo:nil repeats:NO];
Try to adjust the interval(> 0.1) by setting, scheduledTimerWithTimeInterval.
And it's a bad way of blocking the main thread.
Use below blocks approach to "do something after sometime".
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do something taking a long time in background.
[NSThread sleepForTimeInterval:0.2];
// Background thread has finished it's work.
// Call another block on main thread to do UI stuff like updating etc..
dispatch_async(dispatch_get_main_queue(), ^{
// Here you are in the main thread again
// You can do whatever you want
});
});

How to stop responding to a shake before a button is pressed?

i am currently making a iPhone app, and a animation reacts to a small shake, this is my code:
static BOOL SJHShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
double
deltaX = fabs(last.x - current.x),
deltaY = fabs(last.y - current.y),
deltaZ = fabs(last.z - current.z);
return
(deltaX > threshold && deltaY > threshold) ||
(deltaX > threshold && deltaZ > threshold) ||
(deltaY > threshold && deltaZ > threshold);
}
- (id)initWithFrame:(CGRect)frame
{
if (self) {
// Initialization code
}
return self;
}
-(void)awakeFromNib{
[super awakeFromNib];
[UIAccelerometer sharedAccelerometer].delegate = self;
}
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration
*) acceleration {
if (self.lastAction) {
if (hasBeenShaken && SJHShaking (self.lastAction, acceleration, 0.7)) {
hasBeenShaken = YES;
animation.animationImages= [NSArray arrayWithObjects:
[UIImage imageNamed:#"Frame01.png"],
[UIImage imageNamed:#"Frame02.png"],
[UIImage imageNamed:#"Frame03.png"],
[UIImage imageNamed:#"Frame04.png"],
[UIImage imageNamed:#"Frame05.png"],
[UIImage imageNamed:#"Frame06.png"],
[UIImage imageNamed:#"Frame07.png"],
[UIImage imageNamed:#"Frame08.png"],
[UIImage imageNamed:#"Frame09.png"],
[UIImage imageNamed:#"Frame010.png"],
[UIImage imageNamed:#"Frame011.png"],
[UIImage imageNamed:#"Frame012.png"],
[UIImage imageNamed:#"Frame013.png"],
nil];
[animation setAnimationRepeatCount:1];
animation.animationDuration = 1;
[animation startAnimating];
[self performSelector:#selector(didFinishAnimating) withObject:nil
afterDelay:1.0];
bottle.hidden=YES;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/champagne
animate.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer2.numberOfLoops = 0;
audioPlayer2.volume = 1;
if (audioPlayer2 == nil);
else
[audioPlayer2 play];
[audioPlayer3 stop];
back.hidden = YES;
} else if (hasBeenShaken && !SJHShaking(self.lastAction, acceleration, 0.2)) {
hasBeenShaken = NO;
}
}
self.lastAction = acceleration;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)dealloc{
[UIAccelerometer sharedAccelerometer].delegate = nil;
}
On the same view controller I have another page before the animation, now I need them to press the button 'start' and then they can shake, but before they press 'start' if they shake, nothing would happen. How would I do this?
Just have a variable that gets set when they press the button. Then in the method that determines whether a shake has taken place, first check whether the button has been clicked.
For example, in the didAccelerate method, you could first check if the button had been pressed, and if not, just return.

Animate addtosubview for multiple subviews

I am having an array of UIViews and I want to arrange them in a main view in a grid form, and I also wanted it to animate ( ie the grids should appear one by one slowly). Tried animating with in a for loop but didnt work but came up with a work around to achieve something closer to what I want, but just wanted to know if there is a better way to do this. I know there is some property with CAAnimationGroup (begintime) but sure how to connect it to addtosubview. Here is my code, let me know if there is a better way to do it.
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat x = 0;
CGFloat delay = .3;
for(int i=0;i<30;i++)
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(x, 0, 50, 50)];
myView.backgroundColor = [UIColor greenColor];
myView.alpha = 0.0;
x = x + 50+ 2;
[UIView animateWithDuration:0.25 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.view addSubview:myView];
myView.transform = CGAffineTransformMakeScale(0.5, 0.5);
// myView.transform = CGAffineTransformMakeRotation(90);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 animations:^{
myView.transform = CGAffineTransformMakeScale(1.0, 1.0);
myView.alpha = 1.0;
}];
}];
delay = delay + .3;
}
// Do any additional setup after loading the view, typically from a nib.
}
-anoop
Hi,I have also used animation within for loop but it didn't work
properly then I call a method that animate the view , multiple times
and it worked properly.I think this is better way to simply apply
animation. by calling a method.I m putting some code here may it will
help u.
- (void)viewDidLoad
{
radius=82.097;
rotationMultiplier=1;
toAngle = fromAngle = 0;
toStart = NO;
center=CGPointMake(161, 233);
NSString *path = [[NSBundle mainBundle] pathForResource:#"click" ofType:#"mp3"];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.player prepareToPlay];
SingleTouchGesture *wheelRecogniser=[[SingleTouchGesture alloc] initWithTarget:self action:#selector(rotateWheel:)];
[self.view addGestureRecognizer:wheelRecogniser];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void) rotate:(int) time
{
CABasicAnimation *topAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
topAnimation.duration=time;
topAnimation.fromValue= [NSNumber numberWithFloat:fromAngle];
topAnimation.toValue=[NSNumber numberWithFloat:toAngle];
topAnimation.delegate = self;
topAnimation.fillMode = kCAFillModeForwards;
topAnimation.removedOnCompletion = NO;
[wheelImage.layer addAnimation:topAnimation forKey:#"transform"];
fromAngle = toAngle;
}
-(void) rotateWheel:(SingleTouchGesture*) recogniser
{
toAngle += recogniser.rotation;
if (toStart)
{
if (recogniser.state == UIGestureRecognizerStateEnded)
{
NSLog(#"Speed === %f",recogniser.speed);
rotationMultiplier=[self getMultiplier:recogniser.speed];
NSLog(#"Rotation Multiplier..%f",rotationMultiplier);
toStart = NO;
NSLog(#"State Ended");
self.player.currentTime=0;
[self.player play];
if (recogniser.rotation>=0)
{
toAngle =fromAngle+(rotationMultiplier*3.14);
}
else
{
toAngle =fromAngle-(rotationMultiplier*3.14);
}
}
[self rotate:1];
}
else
{
if (recogniser.state==UIGestureRecognizerStateChanged) {
NSLog(#"continue...");
CGPoint pt=[recogniser locationInView:self.view];
float dis=[recogniser distanceBetweenPoints:center with:pt];
if (dis<radius)
{
if (recogniser.rotation>=0)
{
NSLog(#"Rotation value: +ve: %f",recogniser.rotation);
toStart = YES;
toAngle =fromAngle+(rotationMultiplier*3.14);
recogniser.rotation = 0;
}
else
{
NSLog(#"Rotation value: -ve: %f",recogniser.rotation);
toStart = YES;
toAngle=fromAngle-(rotationMultiplier*3.14);
recogniser.rotation = 0;
}
}
}
}
}
Here rotate method is calling multiple times.

animation with infinite scrollView

I have 5 pictures inside an infinite scrollView.
So in order to make that scrollView infinite/circular I positioned my images like this:
5 1 2 3 4 5 1
meaning: last picture first picture second picture.....last picture first picture
And in order for it to become infinite I have the following code:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if(self.scrollView.contentOffset.x == 0){
[self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width*pathImages.count, 0, self.view.frame.size.width, self.view.frame.size.height) animated:NO];
}
else if (self.scrollView.contentOffset.x == self.view.frame.size.width*(pathImages.count+1)) {
[self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];
}
}
which means that when I'm at the last picture the contentOffset of the scrollView is set to go to the first picture-in this way I get an infinite scrollView.
What I wanted next was for my scrollView to slide automatically-for this I set a timer which calls one method-onTimer:
- (void) onTimer{
NSLog(#"flip pages");
if(h < pathImages.count*self.view.frame.size.width)
{
h+= self.view.frame.size.width;
}
else
{
h=self.view.frame.size.width;
}
if(self.scrollView.contentOffset.x == 0){
[self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width*pathImages.count, 0, self.view.frame.size.width, self.view.frame.size.height) animated:NO];
}
if (self.scrollView.contentOffset.x == self.view.frame.size.width*pathImages.count)
{
[self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];
}
else
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{ self.scrollView.contentOffset = CGPointMake(h, 0); }
completion:NULL];
}
This magic line:
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{ self.scrollView.contentOffset = CGPointMake(h, 0); }
completion:NULL];
does the scoll automatically with animation.
Everything is great except this:
after I view the last picture I should set the offset of scroll view in order to get back to the first picture with animation.
Well if I do that:
if (self.scrollView.contentOffset.x == self.view.frame.size.width*pathImages.count)
{
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{ [self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO]; }
completion:NULL];
}
after the last picture is viewed in order to get to the first picture...it loops through all the other pictures.
What I want is this:after I view the last picture to get me back to the first picture which should be loaded on screen using animation, but without viewing all the other pictures between them.Thanks
If I see this correctly, the problem is, that you again use the animation to scroll the view back to zero position. I believe you need to modify the last bit of code you posted to something like this:
if (self.scrollView.contentOffset.x == self.view.frame.size.width*pathImages.count)
{
[self.scrollView scrollRectToVisible:CGRectMake(self.view.frame.size.width,0 ,self.view.frame.size.width, self.view.frame.size.height) animated:NO];//no animation on returning
[self onTimer];//even if this code is already inside method "onTimer"
}
Rather then what you are doing try using a scrollview that displays only 3 to 5 images at the time (if they are fullscreen). If you will have many images in your application, it will crash because of high memory consumption. Try playing with this test example that does nearly what you want:
HEADER:
#import <Foundation/Foundation.h>
#interface IScrollView : UIScrollView <UIScrollViewDelegate> {
NSMutableArray *imagePaths;
UIImageView *imageViews[3];
NSInteger currentImage;
NSTimer *animationTimer;//weak link
}
#end
SOURCE:
#import "IScrollView.h"
#implementation IScrollView
- (UIImage *)imageFromResourcesWithName:(NSString *)name {
UIImage *ret = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name]];
return [ret autorelease];
}
- (id)initWithFrame:(CGRect)frame {
if((self = [super initWithFrame:frame])) {
imagePaths = [[NSMutableArray alloc] init];
[imagePaths addObject:#"imag1.png"];
[imagePaths addObject:#"imag2.png"];
[imagePaths addObject:#"imag3.png"];
[imagePaths addObject:#"imag4.png"];
[imagePaths addObject:#"imag5.png"];
imageViews[0] = [[UIImageView alloc] initWithFrame:CGRectMake(320.0f*0, .0f, 320.0f, 480.0f)];
imageViews[1] = [[UIImageView alloc] initWithFrame:CGRectMake(320.0f*1, .0f, 320.0f, 480.0f)];
imageViews[2] = [[UIImageView alloc] initWithFrame:CGRectMake(320.0f*2, .0f, 320.0f, 480.0f)];
[self addSubview:imageViews[0]];
[self addSubview:imageViews[1]];
[self addSubview:imageViews[2]];
imageViews[0].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:0]];
imageViews[1].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:1]];
imageViews[2].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:2]];
currentImage = 1;
self.contentOffset = CGPointMake(320.0f*currentImage, .0f);
self.contentSize = CGSizeMake(3.0f*320.0f, 480.0f);
self.delegate = self;
animationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:#selector(scrollFragment) userInfo:nil repeats:YES];
}
return self;
}
- (void)repositionIfNeeded {
CGFloat offsetX = self.contentOffset.x;
NSInteger iCount = [imagePaths count];
if(offsetX > 320.0f*1.75f) {
self.contentOffset = CGPointMake(offsetX-320.0f, .0f);
imageViews[0].image = imageViews[1].image;
imageViews[1].image = imageViews[2].image;
NSInteger imageToLoad = currentImage+2;
if(imageToLoad>iCount-1)
imageToLoad -= iCount;
imageViews[2].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:imageToLoad]];
currentImage++;
if(currentImage>iCount-1)
currentImage -= iCount;
}
else if(offsetX < 320.0f*.25f) {
self.contentOffset = CGPointMake(offsetX+320.0f, .0f);
imageViews[2].image = imageViews[1].image;
imageViews[1].image = imageViews[0].image;
NSInteger imageToLoad = currentImage-2;
if(imageToLoad<0)
imageToLoad += iCount;
imageViews[0].image = [self imageFromResourcesWithName:[imagePaths objectAtIndex:imageToLoad]];
currentImage--;
if(currentImage<0)
currentImage += iCount;
}
}
- (void)scrollFragment {
self.contentOffset = CGPointMake(self.contentOffset.x+1.0, .0f);
[self repositionIfNeeded];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self repositionIfNeeded];
}
- (void)dealloc {
[imageViews[0] release];
[imageViews[1] release];
[imageViews[2] release];
[imagePaths release];
}
#end

Xcode: UIScrollView image gallery, having memory problems

I'm Trying to develop an UIScrollView Based Image gallery.
So let me explain what i'm trying to achieve here:
I want to a sliding presentation, that can show upto 140 image. ( You can swipe back and forth )
I've found information on the web, and i've been told the best way to do this is with a UIScrollview which has 3 UIImageViews which you create and remove from superview.
So i managed to create such a "sliding image gallery" with some help from a few tutorials :).
I've managed to upload the application to the ipad, start up the application and run it.
after i viewed about 50-70 slides the app crashes ( out of memory). My knowledge of Obj. C isn't that great .
You'll find the code below: it Prob. has something to do with releasing the images.
Improvements to the code would be really helpful
#import "ParatelPresentationViewController.h"
//Define the UIView ( we need 3 Image Views left, mid right);
#interface SlideShowView : UIView
{
NSArray * mImages;
UIImageView * mLeftImageView;
UIImageView * mCurrentImageView;
UIImageView * mRightImageView;
NSUInteger mCurrentImage;
BOOL mSwiping;
CGFloat mSwipeStart;
}
- (id)initWithImages:(NSArray *)inImages;
#end // SlideShowView
#pragma mark -
#implementation SlideShowView
- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
if (inImageIndex >= [mImages count])
{
return nil;
}
UIImageView * result = [[UIImageView alloc] initWithImage:[mImages objectAtIndex:inImageIndex]];
result.opaque = YES;
result.userInteractionEnabled = NO;
result.backgroundColor = [UIColor blackColor];
result.contentMode = UIViewContentModeScaleAspectFit;
result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;
return result;
}
- (id)initWithImages:(NSArray *)inImages
{
if (self = [super initWithFrame:CGRectZero])
{
mImages = [inImages retain];
NSUInteger imageCount = [inImages count];
NSLog(#"hoeveel foto's: %i");
if (imageCount > 0)
{
mCurrentImageView = [self createImageView:0];
[self addSubview:mCurrentImageView];
if (imageCount > 1)
{
mRightImageView = [self createImageView:1];
[self addSubview:mRightImageView];
}
}
self.opaque = YES;
self.backgroundColor = [UIColor blueColor];
self.contentMode = UIViewContentModeScaleAspectFit;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return self;
}
- (void)dealloc
{
[mImages release];
[super dealloc];
}
- (void)layoutSubviews
{
if (mSwiping)
return;
//CGSize contentSize = self.frame.size; // Enable when you use content.width/height
//self.backgroundColor = [UIColor redColor];
mLeftImageView.frame = CGRectMake(-1024, 0.0f, 1024, 748);// (-1024, 0.0f, 1024, 748) can be replaced by (-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, 1024, 748);
mRightImageView.frame = CGRectMake(1024, 0.0f, 1024, 748);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] != 1)
return;
mSwipeStart = [[touches anyObject] locationInView:self].x;
mSwiping = YES;
mLeftImageView.hidden = NO;
mCurrentImageView.hidden = NO;
mRightImageView.hidden = NO;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (! mSwiping || [touches count] != 1)
return;
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
//CGSize contentSize = self.frame.size;
mLeftImageView.frame = CGRectMake(swipeDistance - 1024, 0.0f, 1024, 748);
mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, 1024, 748);
mRightImageView.frame = CGRectMake(swipeDistance + 1024, 0.0f, 1024, 748);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (! mSwiping)
return;
//CGSize contentSize = self.frame.size;
NSUInteger count = [mImages count];
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
if (mCurrentImage > 0 && swipeDistance > 50.0f)
{
mRightImageView.image = nil;
//[mRightImageView.image release];
[mRightImageView removeFromSuperview];
[mRightImageView release];
//mRightImageView = nil;
//NSLog(#"Count of mRight : %i",[mRightImageView retainCount]);
mRightImageView = mCurrentImageView;
mCurrentImageView = mLeftImageView;
mCurrentImage--;
if (mCurrentImage > 0)
{
mLeftImageView = [self createImageView:mCurrentImage - 1];
mLeftImageView.hidden = YES;
[self addSubview:mLeftImageView];
}
else
{
mLeftImageView = nil;
}
}
else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
{
mLeftImageView.image = nil;
//[mLeftImageView.image release];
[mLeftImageView removeFromSuperview];
[mLeftImageView release];
//mLeftImageView = nil;
mLeftImageView = mCurrentImageView;
mCurrentImageView = mRightImageView;
mCurrentImage++;
if (mCurrentImage < count - 1)
{
mRightImageView = [self createImageView:mCurrentImage + 1];
mRightImageView.hidden = YES;
[self addSubview:mRightImageView];
NSLog(#"Count of mRight : %i",[mRightImageView.image retainCount]);
}
else
{
mRightImageView = nil;
}
}
[UIView beginAnimations:#"swipe" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3f];
mLeftImageView.frame = CGRectMake(-1024, 0.0f, 1024, 748);
mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, 1024, 748);
mRightImageView.frame = CGRectMake(1024, 0.0f, 1024, 748);
[UIView commitAnimations];
mSwiping = NO;
}
#end // SlideShowView
#pragma mark -
#implementation ParatelPresentationViewController
- (id)init
{
if (self = [super initWithNibName:nil bundle:nil])
{
NSMutableArray *Displayimages = [[NSMutableArray alloc]init];
int i;
for(i=0 ; i<139 ; i++) {
NSString *tempString = [NSString stringWithFormat:#"Dia%d", i+1];
NSLog(#"Dia%d.jpg", i+1);
NSString *imageFile = [[NSBundle mainBundle] pathForResource:tempString ofType:#"JPG"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imageFile];
if (fileExists){
[Displayimages addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:tempString ofType:#"JPG"]]];
NSLog(#"img");
}else {
break;
}
}
//NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"], [UIImage imageNamed:#"2.jpg"], [UIImage imageNamed:#"3.jpg"], [UIImage imageNamed:#"4.jpg"], [UIImage imageNamed:#"5.jpg"], nil];
//NSLog(#"Objects Img = %#", images);
NSLog(#"Images %#",Displayimages);
self.view = [[[SlideShowView alloc] initWithImages:Displayimages] autorelease];
[Displayimages release];
}
return self;
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//return YES;
return UIInterfaceOrientationIsLandscape (interfaceOrientation);
}
#end
If you would find a solution or tips, all are welcome !
Thanks in advance
kind regards Bart !
You definitely have a memory leak in createImageView:. You will have to change
return result;
to
return [result autorelease];
There might be more leaks, but that's an obvious one.
Johannes