Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sergio10

macrumors regular
Original poster
Oct 3, 2007
137
0
Hi,

I've implemented custom UIScrollView with images. But on dragging it crashes.
Here is a code:
PHP:
const CGFloat kScrollObjHeight   = 100.0;
const CGFloat kScrollObjWidth    = 100.0;
const NSUInteger kNumImages      = 7;

- (void)viewDidLoad {
	
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];	
    [_scrollView setBackgroundColor:[UIColor whiteColor]];
    [_scrollView setCanCancelContentTouches:NO];
    _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    _scrollView.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
    _scrollView.scrollEnabled = YES;
    _scrollView.minimumZoomScale = 1;
    _scrollView.maximumZoomScale = 3;
    _scrollView.delegate = self;
    [_scrollView setScrollEnabled:YES];	
    [_scrollView setContentSize:[self loadImagesFromBundle]];	
    _scrollView.pagingEnabled = YES;
}

// load all the images from bundle 
// and add them to the scroll view
- (CGSize)loadImagesFromBundle {
	
	CGSize contentSize;
	contentSize.height = kScrollObjHeight;
	contentSize.width = 0;	
	const int kGap = 20;  
	
    for (int i = 1; i <= kNumImages; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"photo%d.png", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
		
        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
        CGRect rect = imageView.frame;

        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
		rect.origin.x = contentSize.width;
		rect.origin.y = kGap;
		
		contentSize.width = contentSize.width + kGap + kScrollObjWidth;
		
        imageView.frame = rect;
        imageView.tag = i;    // tag our images for later use when we place them in serial fashion
        [_scrollView addSubview:imageView];
        [imageView release];
    }	
	
	return contentSize;
}
PHP:
@interface ScrollViewController : UIViewController <UIScrollViewDelegate> {

	IBOutlet UIScrollView *_scrollView;
}

- (CGSize)loadImagesFromBundle;

@property (retain) UIScrollView* _scrollView;
@end
Where is a mistake?

Thank you.
 
This statement is far too vague for us to help diagnose. You need to determine the cause of the crash. For example, have you tried checking the crash logs to see what they say?
Here is a crash report:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000020299028
Crashed Thread: 0

Application Specific Information:
objc_msgSend() selector name: respondsToSelector:
iPhone Simulator 3.1 (139.1), iPhone OS 3.0 (7A341)

Thread 0 Crashed:
0 libobjc.A.dylib 0x92d01ee4 objc_msgSend + 36
1 UIKit 0x3092f97e -[UIScrollView(Static) _smoothScroll:] + 4256
2 UIKit 0x309234a2 ScrollerHeartbeatCallback + 129
3 GraphicsServices 0x32047c8f HeartbeatTimerCallback + 35
4 CoreFoundation 0x302454a0 CFRunLoopRunSpecific + 3696
5 CoreFoundation 0x30244628 CFRunLoopRunInMode + 88
6 GraphicsServices 0x32044c31 GSEventRunModal + 217
7 GraphicsServices 0x32044cf6 GSEventRun + 115
8 UIKit 0x309021ee UIApplicationMain + 1157
9 Connect 0x00002b38 main + 102 (main.m:22)
10 Connect 0x00002aa6 start + 54
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.