- Registriert
- 24.02.11
- Beiträge
- 14
So nun hab ich doch schon wieder ein Problem...
Ich übergebe ne Array mit Bildern aus einer anderen View. Die neue View ist eine Gallery in Form eines Sliders. Jetzt kommt das Problem, dass wenn ich mehr als 10 Bilder habe wovon jedes ca 300 kb hat die App crasht. Ich weiß dass das zuviel an Daten für das Iphone ist, deshalb würde ich gerne die Bilder nur bei Bedarf laden, also z.b. 1 vorheriges und nachfolgendes Bild damit ich den Speicher nicht zumülle. Jetzt meine Frage: wie und wo kann ich bewerkstelligen? Anbei mein Code:
Liebe Grüße und schon einmal Danke für die Unterstützung, leider bin ich immernoch ein Newbie auf dem Gebiet und brauche ab und zu eine genauere Erklärung oder ein Codebeispiel um etwas zu verstehen.
Ich übergebe ne Array mit Bildern aus einer anderen View. Die neue View ist eine Gallery in Form eines Sliders. Jetzt kommt das Problem, dass wenn ich mehr als 10 Bilder habe wovon jedes ca 300 kb hat die App crasht. Ich weiß dass das zuviel an Daten für das Iphone ist, deshalb würde ich gerne die Bilder nur bei Bedarf laden, also z.b. 1 vorheriges und nachfolgendes Bild damit ich den Speicher nicht zumülle. Jetzt meine Frage: wie und wo kann ich bewerkstelligen? Anbei mein Code:
Code:
#import "PicSlideGalleryController.h"
#import "PicGalleryViewController.h"
@interface SnapShowView : UIView
{
NSArray *Images;
UIImageView * LeftImage;
UIImageView * mCurrentImageView;
UIImageView * RightImage;
NSUInteger mCurrentImage;
BOOL mSwiping;
CGFloat mSwipeStart;
}
- (id)initWithImages:(NSArray *)inImages;
@end
@implementation SnapShowView
- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
if (inImageIndex >= [Images count])
{
return nil;
}
UIImageView * result = [[UIImageView alloc] initWithImage:[Images 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])
{
Images = [inImages retain];
NSUInteger imageCount = [inImages count];
if (imageCount > 0)
{
mCurrentImageView = [self createImageView:0];
[self addSubview:mCurrentImageView];
if (imageCount > 1)
{
RightImage = [self createImageView:1];
[self addSubview:RightImage];
}
}
self.opaque = YES;
self.backgroundColor = [UIColor blackColor];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return self;
}
- (void)dealloc
{
[Images release];
[super dealloc];
}
- (void)layoutSubviews
{
if (mSwiping)
return;
CGSize contentSize = self.frame.size;
LeftImage.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
RightImage.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] != 1)
return;
mSwipeStart = [[touches anyObject] locationInView:self].x;
mSwiping = YES;
LeftImage.hidden = NO;
mCurrentImageView.hidden = NO;
RightImage.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;
LeftImage.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, contentSize.width, contentSize.height);
RightImage.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (! mSwiping)
return;
CGSize contentSize = self.frame.size;
NSUInteger count = [Images count];
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
if (mCurrentImage > 0 && swipeDistance > 50.0f)
{
[RightImage removeFromSuperview];
[RightImage release];
RightImage = mCurrentImageView;
mCurrentImageView = LeftImage;
mCurrentImage--;
if (mCurrentImage > 0)
{
LeftImage = [self createImageView:mCurrentImage - 1];
LeftImage.hidden = YES;
[self addSubview:LeftImage];
}
else
{
LeftImage = nil;
}
}
else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
{
[LeftImage removeFromSuperview];
[LeftImage release];
LeftImage = mCurrentImageView;
mCurrentImageView = RightImage;
mCurrentImage++;
if (mCurrentImage < count - 1)
{
RightImage = [self createImageView:mCurrentImage + 1];
RightImage.hidden = YES;
[self addSubview:RightImage];
}
else
{
RightImage = nil;
}
}
[UIView beginAnimations:@"swipe" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3f];
LeftImage.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
RightImage.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
[UIView commitAnimations];
mSwiping = NO;
}
@end
#pragma mark -
@implementation PicSlideGalleryController
@synthesize selectedGalID;
@synthesize gimages;
-(void)viewDidLoad
{
NSLog(@"%@",gimages);
NSMutableArray *zimages = [[[NSMutableArray alloc]init]autorelease];
for(int i = 0; i < gimages.count; ++i) {
NSString *img = [gimages objectAtIndex:i ];
NSString *path = [NSString stringWithFormat:@"http://www.xxx.de/GALLERY/%@/",selectedGalID];
NSString *thumbUrl = [NSString stringWithFormat:@"%@/%@",path,img];
NSData *imageData = [[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:thumbUrl]]autorelease];
UIImage *Img = [[[UIImage alloc]initWithData:imageData]autorelease];
//NSLog(@"%@",Img);
[zimages addObject:Img];
}
self.view = [[[SnapShowView alloc] initWithImages:zimages] autorelease];
}
@end
Liebe Grüße und schon einmal Danke für die Unterstützung, leider bin ich immernoch ein Newbie auf dem Gebiet und brauche ab und zu eine genauere Erklärung oder ein Codebeispiel um etwas zu verstehen.