Screenshot of ADInterstitialAd programmatically in iOS gives black image - ios

When I show the iAd on _parentView(UIView) and create the image of this _parentView.
I show this image (resultingImage) on a UIImageView, then this image is blank(totally black).
How can I get the screenshot of iAd programmatically?
- (id)initWithView:(UIView *)currentView
{
self = [super init];
if (self) {
_parentView = currentView;
self.interstitial = [[ADInterstitialAd alloc] init];
self.interstitial.delegate = self;
}
return self;
}
- (void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd
{
[interstitialAd presentInView:_parentView];
[self takeScreenshot];
}
- (void)takeScreenshot
{
CGRect rect = CGRectMake(0, 0, _parentView.frame.size.width, _parentView.frame.size.height);
UIGraphicsBeginImageContext(rect.size);
[_parentView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

Did you check if the device is a iPad?
Based on the Official Apple document.
It seems like you are doing it correctly. You might wana check if
[interstitialAd presentInView:_parentView];
returns a true of false, before continuing.
If that does not help, try using the delegate function:
- (void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error;
if it returns you any errors.
Edit:
Try using the root view controller from the key window. It works for me.
if([self.interstitial presentInView:_view])
{ NSLog(#"Able to present in view"); }
else
{ [self.interstitial presentFromViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; }

Related

removeFromSuperview ios crashes

My app crashes, not always, at the following method
// overridden
- (void)dismiss
{
[super dismiss];
[containerView_ removeFromSuperview];
containerView_ = nil;
}
crash happens at removerFromSuperview.
There is a "show" method as well
// overridden
- (void)show
{
if (self.parentView == nil)
{
// No parentView, create transparent view as parent
CGSize frameSize = [UIApplication currentSize];
containerView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frameSize.height, frameSize.width)];
containerView_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
containerView_.backgroundColor = [UIColor clearColor];
self.parentView = containerView_;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
if(some condition )
[[UIApplication sharedApplication].keyWindow.subviews.lastObject addSubview:containerView_];
else
[[UIApplication sharedApplication].keyWindow addSubview:containerView_];
}
else
{
[[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:containerView_];
}
}
[super show];
// This is done to allow the Cancel button to be pressed but nothing else - do after [super show]!
self.superview.userInteractionEnabled = YES;
}
It is strange, that code used to work. I am trying to compile the app for arm64, but I don't understand how that modification impacted those methods.
My app is a non-ARC app, and I cannot go to ARC right now.
Any ideas?
Change your code to dismiss view like this.
// overridden
- (void)dismiss
{
if(containerView_)
[containerView_ removeFromSuperview];
containerView_ = nil;
[super dismiss];
}
Please check with below code -
- (void)dismiss
{
if (containerView_)
{
[containerView_ removeFromSuperview];
containerView_ = nil;
[super dismiss];
}
}
Simply check if container view have superview
- (void)dismiss
{
if ([containerView_ superview])
{
[containerView_ removeFromSuperview];
containerView_ = nil;
[super dismiss];
}
}

UIActivityViewController Copy Ignores imageOrientation

When I take a picture with the phone in Portrait mode, using a UIImagePickerController, the UIImage item that is returned has the top of the image facing left and an imageOrientation property of UIImageOrientationRight , // 90 deg CCW, which is as expected (I think).
When I share the image via a UIActivityViewController, all activities seem to behave correctly except for the Copy activity. When I paste the image into another application (for instance, Messages), the image seems to be ignoring the imageOrientation property.
Has anyone else seen this / found a workaround?
Updated with complete code: (project contains storyboard with a single button mapped to self.takeImageButton and - (IBAction)takeImageAndShare:(id)sender;)
#import "ViewController.h"
#interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIButton *takeImageButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)takeImageAndShare:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.popoverPresentationController.sourceView = self.takeImageButton;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
NSLog(#"Image Orientation: %li",chosenImage.imageOrientation);
// Now share the selected image
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[chosenImage] applicationActivities:nil];
activityViewController.modalPresentationStyle = UIModalPresentationPopover;
activityViewController.popoverPresentationController.sourceView = self.takeImageButton;
[self presentViewController:activityViewController animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#end
If you run this code, take a photo with the phone in Portrait (.imageOrientation is 3 = UIImageOrientationRight) and select share activities like Messages, Save Image, etc., the image orientation is correct. If you select the copy activity, the image that gets pasted elsewhere is rotated (top of the image is to the left).
Submitted as Radar 19456881. Complete project is available here.
By Default capture image orientation behavior is UIImageOrientationRight, sp you have to set capture image orientation is
[UIDevice currentDevice].orientation
Based on this post, I decided to create my own copy activity:
//
// PhotoActivityCopy.m
//
// Created by Jeff Vautin on 1/13/15.
// Copyright (c) 2015 Jeff Vautin. All rights reserved.
//
#import "PhotoActivityCopy.h"
#import "ImageKit.h"
#import MobileCoreServices;
#interface PhotoActivityCopy ()
#property (strong,nonatomic) NSData *imageJPEGData;
#end
#implementation PhotoActivityCopy
NSString *PhotoActivityCopyActivityType = #"com.me.uiactivitytype.copy";
NSString *PhotoActivityCopyActivityTitle = #"Copy";
+ (UIActivityCategory)activityCategory
{
return UIActivityCategoryAction;
}
- (NSString *)activityType
{
return PhotoActivityCopyActivityType;
}
- (NSString *)activityTitle
{
return PhotoActivityCopyActivityTitle;
}
- (UIImage *)activityImage
{
CGSize imageSize;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
imageSize = CGSizeMake(76.0, 76.0);
} else {
imageSize = CGSizeMake(60.0, 60.0);
}
return [ImageKit imageOfIconCopyWithExportSize:imageSize];
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
BOOL canPerform = NO;
if ([activityItems count] > 1) {
canPerform = NO;
} else if ([[activityItems firstObject] isKindOfClass:[UIImage class]]) {
canPerform = YES;
}
return canPerform;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
self.imageJPEGData = UIImageJPEGRepresentation(activityItems[0], 1.0);
}
- (void)performActivity
{
[[UIPasteboard generalPasteboard] setData:self.imageJPEGData forPasteboardType:(id)kUTTypeJPEG];
[self activityDidFinish:YES];
}
#end
And then I excluded the native copy activity:
PhotoActivityCopy *copyActivity = [[PhotoActivityCopy alloc] init];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[chosenImage] applicationActivities:#[copyActivity]];
activityViewController.excludedActivityTypes = #[UIActivityTypeCopyToPasteboard];

Load a ViewController code from a xib project imported to a storyboard project.

I have been experimenting with some code in a sample project that uses xibs. In that project the below method takes a photo that was picked via UIImagePickerController and displays another VC modally initializing its scrollview with the image.
This works fine in the xib project but now that im integrating it into a storyboard project this section throws an exception:
SSPhotoCropperViewController *photoCropper =
[[SSPhotoCropperViewController alloc] initWithPhoto:nonRawImage
delegate:self
uiMode:SSPCUIModePresentedAsModalViewController
showsInfoButton:YES];
The error is:
'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/24FB2532-BB1D-4573-8551-386FAA154022/BubbleBoss.app> (loaded)' with name 'SSPhotoCropperViewController''
Which seems to be saying that its looking for the xib file that isnt there. I have created a mimicked storyboard version of the xib and hooked up all my actions and outlets the same.
How do I display a storyboard VC in the below method, set the scrollview photo to nonRawImage, pass it the min and max zoom as is done below?
Problem Method:
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:NO completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
UIImage *nonRawImage=[self scaleAndRotateImage:image];
SSPhotoCropperViewController *photoCropper =
[[SSPhotoCropperViewController alloc] initWithPhoto:nonRawImage
delegate:self
uiMode:SSPCUIModePresentedAsModalViewController
showsInfoButton:YES];
[photoCropper setMinZoomScale:0.25f];
[photoCropper setMaxZoomScale:3.00f];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:photoCropper];
[self presentViewController:nc animated:YES completion:nil];
// photoPreviewImageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}
Code from SSPhotoCropperViewController.m
- (id) initWithPhoto:(UIImage *)aPhoto
delegate:(id<SSPhotoCropperDelegate>)aDelegate
{
return [self initWithPhoto:aPhoto
delegate:aDelegate
uiMode:SSPCUIModePresentedAsModalViewController
showsInfoButton:YES];
}
- (id) initWithPhoto:(UIImage *)aPhoto
delegate:(id<SSPhotoCropperDelegate>)aDelegate
uiMode:(SSPhotoCropperUIMode)uiMode
showsInfoButton:(BOOL)showsInfoButton
{
if (!(self = [super initWithNibName:#"SSPhotoCropperViewController" bundle:nil])) {
return self;
}
self.photo = aPhoto;
self.delegate = aDelegate;
_uiMode = uiMode;
_showsInfoButton = showsInfoButton;
self.minZoomScale = 0.5f;
self.maxZoomScale = 3.0f;
self.infoMessageTitle = #"In order to crop the photo";
self.infoMessageBody = #"Use two of your fingers to zoom in and out the photo and drag the"
#" green window to crop any part of the photo you would like to use.";
self.photoCropperTitle = #"Crop Photo";
return self;
}
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.photo = nil;
self.delegate = nil;
}
return self;
}
- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction) infoButtonTapped:(id)sender
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:self.infoMessageTitle
message:self.infoMessageBody
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[av show];
}
#pragma -
#pragma mark - View lifecycle
- (void) viewDidLoad
{
[super viewDidLoad];
//
// setup view ui
//
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(saveAndClose:)];
self.navigationItem.rightBarButtonItem = bi;
if (_uiMode == SSPCUIModePresentedAsModalViewController) {
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAndClose:)];
self.navigationItem.leftBarButtonItem = bi;
}
if (!_showsInfoButton) {
[self.infoButton setHidden:YES];
}
self.title = self.photoCropperTitle;
//
// photo cropper ui stuff
//
[self setScrollViewBackground];
[self.scrollView setMinimumZoomScale:self.minZoomScale];
[self.scrollView setMaximumZoomScale:self.maxZoomScale];
[self.cropRectangleButton addTarget:self
action:#selector(imageTouch:withEvent:)
forControlEvents:UIControlEventTouchDown];
[self.cropRectangleButton addTarget:self
action:#selector(imageMoved:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
if (self.photo != nil) {
[self loadPhoto];
}
}
- (void) viewDidUnload
{
[super viewDidUnload];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
#pragma -
#pragma UIScrollViewDelegate Methods
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
#pragma -
#pragma Private Methods
- (void) loadPhoto
{
if (self.photo == nil) {
return;
}
CGFloat w = self.photo.size.width;
CGFloat h = self.photo.size.height;
CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
self.scrollView.contentSize = imageViewFrame.size;
UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
iv.image = self.photo;
[self.scrollView addSubview:iv];
self.imageView = iv;
}
Ok ive got the method call changed to
SSPhotoCropperViewController *photoCropper =
[[SSPhotoCropperViewController alloc] initWithPhoto:photo
delegate:self
uiMode:SSPCUIModePresentedAsModalViewController
showsInfoButton:YES];
[photoCropper setMinZoomScale:0.25f];
[photoCropper setMaxZoomScale:3.00f];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:photoCropper];
But Im not sure what to do with the below:
- (id) initWithPhoto:(UIImage *)aPhoto
delegate:(id<SSPhotoCropperDelegate>)aDelegate
uiMode:(SSPhotoCropperUIMode)uiMode
showsInfoButton:(BOOL)showsInfoButton
{
if (!(self = [super initWithNibName:#"SSPhotoCropperViewController" bundle:nil])) {
return self;
}
self.photo = aPhoto;
self.delegate = aDelegate;
_uiMode = uiMode;
_showsInfoButton = showsInfoButton;
self.minZoomScale = 0.5f;
self.maxZoomScale = 3.0f;
self.infoMessageTitle = #"In order to crop the photo";
self.infoMessageBody = #"Use two of your fingers to zoom in and out the photo and drag the"
#" green window to crop any part of the photo you would like to use.";
self.photoCropperTitle = #"Crop Photo";
return self;
}
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.photo = nil;
self.delegate = nil;
}
return self;
}
You should give your view controller an identifier within the storyboard, then instantiate it as follows:
YourViewControllerClass *viewController =
[[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:#"ViewController"];
The identifier can be set using Storyboard ID from within the Identity Inspector.
Make sure you set the SSPhotoCropperViewController's Identifier in the storyboard to SSPhotoCropperViewController.
Change your method:
- (id) initWithPhoto:(UIImage *)aPhoto
delegate:(id<SSPhotoCropperDelegate>)aDelegate
uiMode:(SSPhotoCropperUIMode)uiMode
showsInfoButton:(BOOL)showsInfoButton
{
//if (!(self = [super initWithNibName:#"SSPhotoCropperViewController" bundle:nil])) {
if (!(self = [[UIStoryboard storyboardWithName:#"YourStoryBoardName" bundle:nil]
instantiateViewControllerWithIdentifier:#"SSPhotoCropperViewController"])) {
return self;
}
self.photo = aPhoto;
self.delegate = aDelegate;
_uiMode = uiMode;
_showsInfoButton = showsInfoButton;
self.minZoomScale = 0.5f;
self.maxZoomScale = 3.0f;
self.infoMessageTitle = #"In order to crop the photo";
self.infoMessageBody = #"Use two of your fingers to zoom in and out the photo and drag the"
#" green window to crop any part of the photo you would like to use.";
self.photoCropperTitle = #"Crop Photo";
return self;
}

Restrict my project to portrait only leaving photo view's in landscape/portrait

I want my whole project to run in portrait mode only and only the view that's for viewing the photo's can turn on landscape same as if using the facebook (we view photo's they turn landscape also but other view's remains in portrait) i want to know that how it's done as i have a table view that has images that i get from the DB from the server and every particular data contains different number's of photo's so now i want that after the user select's the photo's they should open up fully and can be viewed in landscape and in portrait as well i did tried
I am working with ios6
- (BOOL)shouldAutorotate
{
return NO;
}
implementing in all the view's so that they can remain in portrait but still they turn in landscape how should i restrict it and please can any one tell me that in one of my class i have images in table view(loaded from the DB) and i have another class that open's up the selected photo from the table
My PhotoView.m class
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [photos objectAtIndex:indexPath.row];
NSLog(#" *** url is %#",[dic objectForKey:#"url"]);
[self.delegate showPhotoAtUrl:[dic objectForKey:#"url"]];
}
My PhotoViewController.h class
#import <UIKit/UIKit.h>
#interface PhotoViewController : UIViewController <UIScrollViewDelegate>
{
NSString *url;
UIButton *doneButton;
}
#property (nonatomic, retain) UIImageView *imageView;
- (id) initWithPhotoUrl:(NSString *)photoUrl;
#end
and PhotoViewController.m class
#import "PhotoViewController.h"
#interface PhotoViewController ()
#end
#implementation PhotoViewController
#synthesize imageView;
- (id) initWithPhotoUrl:(NSString *)photoUrl
{
self = [super init];
if(self) {
url = [photoUrl retain];
}
return self;
}
- (void)dealloc
{
[url release];
self.imageView = nil;
[doneButton release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.tag = 1;
spinner.center = self.view.center;
[spinner startAnimating];
[self.view addSubview:spinner];
[spinner release];
[self performSelectorInBackground:#selector(loadPhotoData) withObject:nil];
doneButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[doneButton addTarget:self action:#selector(doneTouched) forControlEvents:UIControlEventTouchUpInside];
[doneButton setTitle:#"done" forState:UIControlStateNormal];
[doneButton setBackgroundColor:[UIColor clearColor]];
doneButton.frame = CGRectMake(2, 2, 60, 30);
[self.view addSubview:doneButton];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.title = #"";
self.navigationController.navigationBarHidden = YES;
}
- (void) doneTouched
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void) loadComplete:(NSData *)imageData
{
if(!imageData) return;
UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)[self.view viewWithTag:1];
if(spinner) {
[spinner stopAnimating];
[spinner removeFromSuperview];
}
UIImage *img = [UIImage imageWithData:imageData];
float scale = MIN(self.view.frame.size.width/img.size.width, self.view.frame.size.height/img.size.height);
self.imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width*scale, self.imageView.image.size.height*scale);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.imageView.center = scrollView.center;
scrollView.delegate = self;
scrollView.contentSize = self.imageView.frame.size;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 2;
[scrollView addSubview:self.imageView];
[self.view addSubview:scrollView];
[scrollView release];
[self.view bringSubviewToFront:doneButton];
}
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void) loadPhotoData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[self performSelectorOnMainThread:#selector(loadComplete:) withObject:imageData waitUntilDone:NO];
[pool drain];
}
#end
and in the main class i have have this method that calls the PhotoViewController to load the selected photo
- (void) showPhotoAtUrl:(NSString *)url
{
PhotoViewController *vc = [[PhotoViewController alloc] initWithPhotoUrl:url];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
Now my problem is how should i get the images after getting selected from the table to be open up in the same type of the view that open's up in FB app (open's the selected photo in portrait/landscape compatible view) i am only able to get one photo in my PhotoViewController class can any one tell me how can i add all the photo's to the PhotoViewController class so that i get the effect that i want ?... Any code help will be very helpful .. Thanks in Advance for contributing your time
In Short i have two things to do :
I have to restrict my project to only portrait mode leaving only the photo's view to have landscape/portrait compatibility.
I am currently having the photo's in my table view that i want on selection to open up in the same style(landscape/portrait compatibile) as FB or other app's do while viewing the photo's.
These only work on iOS 6.0
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
The method will return which orientation mode to use first.
Below are supportedInterfaceOrientations:
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
For iOS > 4.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Other ViewControllers will have
-(BOOL)shouldAutorotate { return NO; }
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation{
return NO;
}

UIInterfaceOrientation remains in portrait

I've got a header/brand image which changes between iPad and iPhone, but will not change the picture when changing from portrait to landscape, which is really important that it does.
I have this HeaderView Class, which is called by tableViewControllers like this:
self.tableView.tableHeaderView = [[HeaderView alloc] initWithText:#""];
which holds a UIImageView:
#interface HeaderView : UIImageView{
}
- (id)initWithText:(NSString*)text;
- (void)setText:(NSString*)text;
#end
For the M file, we find where I'm not getting the result I want:
#import "HeaderView.h"
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
#interface HeaderView()
{
UILabel*label;
}
#end
#implementation HeaderView
- (id)initWithText:(NSString*)text
{
UIImage* img = [UIImage imageNamed:#"WashU.png"];
UIImage* iPhonePortrait = [UIImage imageNamed:#"WashU2.png"];
UIImage* image = [[UIImage alloc] init];
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
//different header image for different devices...
if(IDIOM==IPAD){
image = img;
}
else{
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
image = iPhonePortrait;
}
else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
iPhonePortrait = nil;
image = img;
}
}
[headerImageView setImage:image];
if (self = [super initWithImage:image]){
}
return self;
}
I also have added these methods:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
I'm sure its a classic sort of question, but I'm reasonably stumped. Also, if I remove
[headerImageView setImage:image];
, and
[self addSubview:headerImageView];
, the image still shows up, which means that
if (self = [super initWithImage:image])
is doing all the display work.
didRotateFromInterfaceOrientation is sent to your view controller after rotation, you can implement that to change your image.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
...change your image here
}
As your code, you do not need allocate (UIImage*)image property, because you will assign (UIImage*)img or (UIImage*)iPhonePortrait. And should assign image to headerImageView.image.
[headerImageView setImage:image];
You are not actually assigning the image to the imageView. Add:
headerImageView.image = image;
somewhere after 'image' has been set (based on iPad vs iPhone and Portrait vs Landscape).

Resources