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

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I have an application, and I have a problem with scroll views and image views.

My purpose is simple: I have an image, and I want to display it on the screen, while providing zooming and rotating capabilities. Unfortunately, I can't do it the normal way, since an (Apple's??) bug forbids me of implementing autorotation in my application. So it seems I must do it the hard way.

So I have added some buttons on the bottom that will rotate the image, and then revert it to original.

This is what I have done so far...

Code:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
[scrollView1 setBackgroundColor:[UIColor blackColor]];
	scrollView1.clipsToBounds = YES;
	scrollView1.canCancelContentTouches = NO;
	scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
	//scrollView.delegate = self;
	scrollView1.maximumZoomScale = 2.0;
	scrollView1.minimumZoomScale = 1.0;
	
	self.navigationController.navigationBar.hidden = YES;
	
	self.imageView = [[UIImageView alloc]initWithImage:[self imageForManagedObjectAtIndex:self.currentImageIndex]];
	if (imageView.image == nil) {
		NSLog(@"image is nil!!");
	}
	[scrollView1 addSubview:imageView];
	[scrollView1 setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
	[scrollView1 setScrollEnabled:YES];
	[imageView release];
	
	[self revertToNormal:nil];


- (IBAction)rotateLeft:(id)sender
{
	[self revertToNormal:nil];
	[self.scrollView1 setZoomScale:1.0 animated:NO];
	CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
	[self.scrollView1 setTransform:transform];
	CGRect scrollViewRect = self.scrollView1.frame;
	scrollViewRect.size.width = 320;
	scrollViewRect.size.height = 480;
	scrollViewRect.origin.x = 0;
	scrollViewRect.origin.y = 0;
	[self.scrollView1 setFrame:scrollViewRect];
	//[self.scrollView setContentSize:scrollViewRect.size];
	
	CGRect imgViewRect = self.imageView.frame;
	imgViewRect.size.height = 320;
	imgViewRect.size.width = 480;
	
	
	
	float imageWidth = imageView.image.size.width;
	float imageHeight = imageView.image.size.height;
	
	NSLog(@"img size: x = %f, y= %f", imageView.image.size.width, imageView.image.size.height );
	
	if (imageHeight > 320) {
		float newimageHeight = 320;
		float newImageWidth = imageWidth * newimageHeight / imageHeight;
		
		imgViewRect.size = CGSizeMake(newImageWidth, newimageHeight);
		//imgViewRect.origin.x += 150;
	}else{
		imgViewRect.size.height = 320;
		imgViewRect.size.width = 480;
	}
	
	
	[self.imageView setFrame:imgViewRect];
	[self.scrollView1 setContentSize:CGSizeMake(imgViewRect.size.width, imgViewRect.size.height)]; //???
	
}

- (IBAction)rotateRight:(id)sender
{
	[self revertToNormal:nil];
	[self.scrollView1 setZoomScale:1.0 animated:NO];
	CGAffineTransform transform = CGAffineTransformMakeRotation(3*M_PI/2);
	[self.scrollView1 setTransform:transform];
	CGRect scrollViewRect = self.scrollView1.frame;
	scrollViewRect.size.width = 320;
	scrollViewRect.size.height = 480;
	scrollViewRect.origin.x = 0;
	scrollViewRect.origin.y = 0;
	[self.scrollView1 setFrame:scrollViewRect];

	CGRect imgViewRect = self.imageView.frame;
	//imgViewRect.size.height = 320;
	//imgViewRect.size.width = 480;
	float imageWidth = imageView.image.size.width;
	float imageHeight = imageView.image.size.height;
	
	NSLog(@"img size: x = %f, y= %f", imageView.image.size.width, imageView.image.size.height );
	
	if (imageHeight > 320) {
		float newimageHeight = 320;
		float newImageWidth = imageWidth * newimageHeight / imageHeight;
		
		imgViewRect.size = CGSizeMake(newImageWidth, newimageHeight);
		//imgViewRect.origin.x += 150;
	}else{
		imgViewRect.size.height = 320;
		imgViewRect.size.width = 480;
	}
	
	
	[self.imageView setFrame:imgViewRect];
	[self.scrollView1 setContentSize:CGSizeMake(imgViewRect.size.width, imgViewRect.size.height)]; //???
	
}

- (IBAction)revertToNormal:(id)sender
{
	//[self.scrollView setZoomScale:1.0 animated:NO];
	CGAffineTransform transform = CGAffineTransformMakeRotation(0);
	
	//CGAffineTransform transform = CGAffineTransformIdentity;
	
	[self.scrollView1 setTransform:transform];
	CGRect scrollViewRect = self.scrollView1.frame;
	scrollViewRect.size.width = 320;
	scrollViewRect.size.height = 480;
	scrollViewRect.origin.x = 0;
	scrollViewRect.origin.y = 0;
	[self.scrollView1 setFrame:scrollViewRect];
	
	CGRect imgViewRect = self.imageView.frame;
	float imageWidth = imageView.image.size.width;
	float imageHeight = imageView.image.size.height;
	
	NSLog(@"img size: x = %f, y= %f", imageView.image.size.width, imageView.image.size.height );
	//imgViewRect.size = self.imageView.image.size;
	imgViewRect = CGRectMake(0, 0, imageWidth, imageHeight);
	if (imageWidth > 320) {
		float newImageWidth = 320;
		float newimageHeight = imageHeight * newImageWidth / imageWidth;
		imgViewRect.size = CGSizeMake(320, newimageHeight);
	}
	
	[self.imageView setFrame:imgViewRect];
	[scrollView1 setContentSize:CGSizeMake(imageView.frame.size.height, imageView.frame.size.width)];
	//[scrollView setContentSize:CGSizeMake(320, 1000)];
}

#pragma mark -
#pragma mark Scroll View Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
	return self.imageView;
}

However, although while rotating the image is properly displayed, while being zoomed it is misplaced. It seems as if the corners of the image view are stuck on the corners of the scroll view, thus leaving black space above.

Can you show me the way of rotating an image the right way without having to use autorotation?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.