2010-08-20 7 views
0

앱이 처음으로 휴대 전화를 회전 할 때만 종료됩니다 (EXC_BAD_ACCESS). 언제든지 그 그레이비를 먹을 수 있습니다. 그것은 장치에서만 너무 충돌합니다. 시뮬레이터의 모든 것이 좋습니다. viewDidLoad에서처음으로 전화를 돌릴 때 앱이 다운 됨

#import "ImageViewController.h" 
#define degreesToRadian(x) (M_PI * (x)/180.0) 
#define ZOOM_VIEW_TAG 100 
#define ZOOM_STEP 1.5 

@interface ImageViewController (UtilityMethods) 
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center; 
@end 

@implementation ImageViewController 
@synthesize imageView,url, enableLandscapeOrientation; 

/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // set up main scroll view 
    imageScrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]]; 
    [imageScrollView setBackgroundColor:[UIColor blackColor]]; 
    [imageScrollView setDelegate:self]; 
    [imageScrollView setBouncesZoom:YES]; 
    [[self view] addSubview:imageScrollView]; 

    // add touch-sensitive image view to the scroll view 
    imageView = [[AsyncImageView alloc]initWithFrame:[self.view bounds]]; 
    if (enableLandscapeOrientation) { 
     imageView.showGhost = YES; 
    } 
    [imageView loadImageFromURL:self.url]; 
    [imageView setTag:ZOOM_VIEW_TAG]; 
    [imageView setUserInteractionEnabled:YES]; 
    [imageScrollView setContentSize:[imageView frame].size]; 
    [imageScrollView addSubview:imageView]; 
    [imageView release]; 

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 
    UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)]; 

    [doubleTap setNumberOfTapsRequired:2]; 
    [twoFingerTap setNumberOfTouchesRequired:2]; 

    [imageView addGestureRecognizer:singleTap]; 
    [imageView addGestureRecognizer:doubleTap]; 
    [imageView addGestureRecognizer:twoFingerTap]; 

    [singleTap release]; 
    [doubleTap release]; 
    [twoFingerTap release]; 

    // calculate minimum scale to perfectly fit image width, and begin at that scale 
    float minimumScale = [imageScrollView frame].size.width/[imageView frame].size.width; 
    [imageScrollView setMinimumZoomScale:minimumScale]; 
    [imageScrollView setMaximumZoomScale:4.0f]; 
    //[imageScrollView setZoomScale:minimumScale]; 

    //[imageScrollView setMinimumZoomScale:0.5f]; 
    //[imageScrollView setMaximumZoomScale:2.0f]; 



    //[self.imageView loadImageFromURL:self.url]; 
} 



// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 

     imageScrollView.frame = CGRectMake(0,0,480,300); //self.view.bounds; 
     imageView.frame = CGRectMake(0,0,480,300); //self.view.bounds; 
    } 
    else { 
     imageScrollView.frame = CGRectMake(0,0,320,460); 
     imageView.frame = CGRectMake(0,0,320,460); 
    } 


    //return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    if (enableLandscapeOrientation) { 
     [[self navigationController] setNavigationBarHidden:UIInterfaceOrientationIsLandscape(interfaceOrientation) animated:YES]; 
     return YES; 
    } 
    else { 
     return NO; 
    } 
} 


- (void)dealloc { 
    [imageView release];imageView=nil; 
    [url release];url=nil; 
    [super dealloc]; 
} 

#pragma mark UIScrollViewDelegate methods 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return [imageScrollView viewWithTag:ZOOM_VIEW_TAG]; 
} 

/************************************** NOTE **************************************/ 
/* The following delegate method works around a known bug in zoomToRect:animated: */ 
/* In the next release after 3.0 this workaround will no longer be necessary  */ 
/**********************************************************************************/ 
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { 
    [scrollView setZoomScale:scale+0.01 animated:NO]; 
    [scrollView setZoomScale:scale animated:NO]; 
} 

#pragma mark Utility methods 

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { 

    CGRect zoomRect; 

    // the zoom rect is in the content view's coordinates. 
    // At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds. 
    // As the zoom scale decreases, so more content is visible, the size of the rect grows. 
    zoomRect.size.height = [imageScrollView frame].size.height/scale; 
    zoomRect.size.width = [imageScrollView frame].size.width/scale; 

    // choose an origin so as to get the right center. 
    zoomRect.origin.x = center.x - (zoomRect.size.width/2.0); 
    zoomRect.origin.y = center.y - (zoomRect.size.height/2.0); 

    return zoomRect; 
} 

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer { 
    // single tap does nothing for now 
} 

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer { 
    // double tap zooms in 
    float newScale = [imageScrollView zoomScale] * ZOOM_STEP; 
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; 
    [imageScrollView zoomToRect:zoomRect animated:YES]; 
} 

- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer { 
    // two-finger tap zooms out 
    float newScale = [imageScrollView zoomScale]/ZOOM_STEP; 
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; 
    [imageScrollView zoomToRect:zoomRect animated:YES]; 
} 




@end 
+0

충돌을 게시하십시오. – bbum

+0

및 imageView 및 imageScrollView – jmont

+0

전체 코드를 게시하는 곳의 게시물을 게시하십시오. 크래시가 크래시 로그에 있었고 EXC_BAD_ACCESS를 제외한 읽을 수있는 데이터가 없었습니다 –

답변

1

당신은 할당 imageView과 끝에 imageView의 유지 카운트가 0 imageView이 해제되었습니다, 그래서 당신은 그것을 할당을 해제, 당신은 빨리 당신이 imageView에 액세스 할로 EXC_BAD_ACCESS을받을 이유입니다 다음 번에. [imageView dealloc]viewDidLoad 메소드에서 제외하면 모든 것이 잘 작동합니다.

또 다른 주목할 점은 스크롤 뷰를 -(void)dealloc에 할당하는 것을 잊어 버리는 것을 잊었습니다.

+0

그래서 기본적으로 이미 viewDidLoad에서 release하고 있기 때문에 dealloc()에서 imageView를 릴리즈하지 않습니까? –

+0

dealloc()에서 imageView 만 릴리스해야합니다. – burki

관련 문제