When I rotate my app the background image will not rotate and
I wish that my background image will rotate.
The content do changes the orientation but the background image stays and will not rotate.
Any ideas on a go around?
Thanks.
The idea is to apply a graphics "transform" to modify your image when the device is rotated. See this answer here... In your image controller, add the following code (where backgroundImage is a property containing your image.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
backgroundImage.transform = CGAffineTransformMakeRotation(M_PI / 2);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
backgroundImage.transform = CGAffineTransformMakeRotation(-M_PI / 2);
}
else {
backgroundImage.transform = CGAffineTransformMakeRotation(0.0);
}
}
Related
I am merging multiple videos and I want to detect which ones are in portrait mode and rotate them in landscape so that all movies are in landscape mode... I have done everything and works perfectly except the actual rotate, I guess it's something with the center of the rotation or a composed rotation.
AVMutableVideoCompositionLayerInstruction *videoTrackLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationPortrait)
{
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI/2);
//CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(640, 480);
//CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter);
[videoTrackLayerInstruction setTransform:rotation atTime:nextClipStartTime];
}
else if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationPortraitUpsideDown)
{
CGAffineTransform rotation = CGAffineTransformMakeRotation(-M_PI/2);
[videoTrackLayerInstruction setTransform:rotation atTime:nextClipStartTime];
}
else if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationLandscapeLeft)
{
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);
[videoTrackLayerInstruction setTransform:rotation atTime:nextClipStartTime];
}
else if([[AppDelegate sharedInstance] orientationForTrack:avAsset] == UIDeviceOrientationLandscapeRight)
{
[videoTrackLayerInstruction setTransform:CGAffineTransformIdentity atTime:nextClipStartTime];
}
How can I rotate them properly? I have tried multiple sources but nothing rotates them as they should. I am not interested in the 320 scale fit solution I want the video to keep as much resolution as possible before exporting using AVAssetExportSession.
A solution like this:
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotateTransform,320,0);
won't suit my needs as I have tried it. Do you have any ideas? Some help will be pretty much appreciated.
I'm using ZBar to detect codes but also, I want to enable taking pictures from the same screen. I detected an odd behaviour taking pictures in landscape orientation. If I put my mobile in vertical landscape position the image comes out ok, but If I move my iPhone to the flat landscape position, the picture comes out upside down. I checked UIImage metadata and the image orientation has different values, despite the fact that the device orientation is the same in both cases.
Any idea why this happens?
My solution is to change the image orientation metadata in the wrong cases:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[....]
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
if(image){
// This fixes a bug in ZBarReader taking picture in landscape orientation and device in flat position.
NSLog(#"Image: %d, Device: %d",image.imageOrientation,self.interfaceOrientation);
UIImageOrientation imgOrientation = image.imageOrientation;
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft && imgOrientation == UIImageOrientationUp){
image = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationDown];
}else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight && imgOrientation == UIImageOrientationDown){
image = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationUp];
}
[self hideScanner];
[self performSegueWithIdentifier:#"mySegue" sender:image];
}
}
}
}
I have a UIView called character. Inside that view the user can add several UIImageView as subViews.
I created gesture methods for Pinch, Move and Rotate. The gesture method were added to the character view.
For moving I'm using the gesture translationInView property and it works fine!
My problem is with scalling.
Scaling individual UIImageViews is ok, no issues, but how to scale all of them??
I tried to scale the character view, it works fine scaling proportionally all the subviews but I don't want to change the character view, because I use it as a canvas to place the UIImageView to compose my character. If I change the character with a CGAffineTransformScale, all the new ImageViews that I add, got the same scale.
I tried this but it doesn't work:
if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) {
CGFloat customScale = gesture.scale;
for (UIImageView *singleView in [self.devoCharacter subviews]) {
singleView.frame = CGRectMake(singleView.frame.origin.x / customScale, singleView.frame.origin.y /customScale, singleView.frame.size.width /customScale , singleView.frame.size.height /customScale);
[gesture setScale:1];
}
}
I wanted to reduce the size of each view and their relative distances in this way, but the whole thing expands from the coordinates origin.
Then I tried this:
if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) {
CGFloat customScale = gesture.scale;
for (UIImageView *singleView in [self.devoCharacter subviews]) {
singleView.transform = CGAffineTransformScale(singleView.transform, customScale, customScale);
[gesture setScale:1];
}
}
But this scales each view independently and does not maintain their relative distances.
Is there anyway to just scale each view, proportionally? and maintaining their relative distances as well?
Ok I got how to do it. Scale every single view and then scale proportionally their centers. This solved my problem:
for (Part *singleView in [self.canvas subviews]) {
singleView.transform = CGAffineTransformScale(singleView.transform, customScale, customScale);
singleView.center = CGPointMake(singleView.center.x * customScale, singleView.center.y * customScale);
[gesture setScale:1];
}
I have customized the camera with custom buttons(UIButton) by overlay view. My iPhone application will run only in Landscape mode(Left and Right). Now, my camera overlay view change it's orientation left and right perfectly in iPhone 3GS but not run clearly in iPhone4. In ios5 if i launch the camera with overlay view in landscapemode(Right/Left) it launching perfectly but, if i change the orientation to (Right -> Left / Left -> Right) the overlay view not changing perfectly the frame size of the UIView(CameraOverlayView),UIButton frame sizes are changing. How can i fix it? Please help me to fix this problem. I hope you my friends fix my problem. Thanks in advance. Here i have attached my code that i used.
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft)
{
CGAffineTransform transform = self.view.transform;
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
imgpicker.cameraOverlayView.transform = transform;
NSLog(#"Orientation Landscape Left");
}
else if(orientation == UIDeviceOrientationLandscapeRight)
{
imgpicker.cameraOverlayView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 117.81);
NSLog(#"Orientation Landscape Right");
}
This code works good in iPhone#GS(ios4) but not working good in iPhone4(ios5). Can anyone please help me to fix this problem?
Thank you my friends. I have solved this issue by myself. I just set the bounds to UIView so it covers full view on Camera OverlayView and also i have tried above code that i mentioned in my question.
cameraview.bounds = CGRectMake(0.0, 0.0, 480.0f, 320.0f);
And so i have checked this code at beginning of Camera launch,
UIInterfaceOrientation orientation= [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
imgpicker.cameraOverlayView.transform = CGAffineTransformRotate(CGAffineTransformIdentity,117.81);
}
else if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGAffineTransform transform = self.view.transform;
transform = CGAffineTransformRotate(transform, (M_PI/2.0));
imgpicker.cameraOverlayView.transform = transform;
}
After this i was check this condition in UIDeviceOrientation Notification also. Now, my app working charm. Thanks.
I load photo from iPhone/iPad library, most of them in Portrait mode, I want to know How can I check the photo in Landscape or Portrait mode?
Use the imageOrientation property of UIImage instances. It is going to return you one of these constants.
Example:
UIImage *image = // loaded from library
if (image.imageOrientation == UIImageOrientationUp) {
NSLog(#"portrait");
} else if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
NSLog(#"landscape");
}
I tested this piece of code on tens of actual picture on iPhone 4 running iOS 5.0 and was able to successfully make them all in portrait mode. This is how you fix/check
if (image.imageOrientation == UIImageOrientationUp ||
image.imageOrientation == UIImageOrientationDown )
{
NSLog(#"Image is in Landscape Fix it to portrait ....");
backgroundView.frame = self.view.bounds;
backgroundView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
backgroundView.contentMode = UIViewContentModeScaleAspectFill;
}
else
{
NSLog(#"Image is in Portrait everything is fine ...");
}
Here is a fool proof way of doing this check
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
// Get the data for the image
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
if ([UIImage imageWithData:imageData].size.width > [UIImage imageWithData:imageData].size.height)
{
NSLog(#"Select Image is in Landscape Mode ....");
}
else
{
NSLog(#"Select Image is in Portrait Mode ...");
}
}