2011-01-18 8 views
1

UIScrollViewベースの画像ギャラリーを開発しようとしています。Xcode:UIScrollView画像ギャラリー、メモリの問題があります

だから、私はここで達成しようとしているかを説明しましょう:

私は140件まで画像表示することができ、スライドプレゼンテーション、したいです。 私はウェブ上の情報を見つけました。これを行うための最善の方法は、UIScrollviewにあり、それは3つのUIImageViewsを作成してスーパービューから削除するというものです。

私はいくつかのチュートリアルの助けを借りて、このような「スライド画像ギャラリー」を作成することができました:) 私はアプリケーションをipadにアップロードし、アプリケーションを起動して実行することができました。

約50-70枚のスライドを見た後、アプリがクラッシュしました(メモリ不足)。 Objの私の知識。 Cはそれほど素晴らしいものではありません。

あなたは以下のコードを見つけるでしょう:Prob。画像を公開することと関係があります。あなたはすべてが歓迎され、溶液またはヒントを見つけたい場合、コードへ 改善は

#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 

本当に参考になります!

ありがとうございます。

種類Bart!

+0

ちょうど '代わりに新しいものをリリースして作成するのUIImageViewのimage'フィールドを変更することができます。 – jamapag

+1

Xcodeのスタティックアナライザは、これらのようなメモリリークをキャッチするための優れたツールです。 Xcode 4では、[製品]メニューから[分析]を選択して実行できます。 – Mattia

+0

UICollectionViewを使用してイメージを表示する必要があります。再利用可能なビューを提供し、一度にすべての画像をメモリにロードする必要はありません。 – gagarwal

答えて

1

あなたは間違いなくcreateImageView:でメモリリークを起こしています。あなたはより多くの漏れがあるかもしれません

return result; 

return [result autorelease]; 

に変更する必要がありますが、それは明らかに一つです。

  • ヨハネス
関連する問題