Im working on Xamarin Studio, and I have a Storyboard which contains a UIImageView, its properties are set like this :
You can notice that chosen image is called personalPhoto which exists in the Images.xcassets like this
But when I run the app in the simulator, the image isn't loaded .. and the UIImageView looks empty
Even if try to add the UIImageView programatically using this code.. it gives me the same result
UIImageView personImage = new UIImageView {
Frame = new CoreGraphics.CGRect (0, 0, 200, 200),
Image = UIImage.FromBundle("personalPhoto"),
ContentMode = UIViewContentMode.ScaleAspectFill
};
this.View.AddSubview (personImage);
So do you have any idea what is the problem .. please give me some help ..
And thanks in advance ..
Restart your iPhone or Simulator and try again.
You can also clean (Product -> clean) and try again
Related
I am relatively new to iOS so go easy on me! In my app I need to have a button which contains an image a title. The image needs to go above the title. The image is defined dynamically from a base64 string.
My problem is I can’t work out how to display the title and the image in the button at the same time. I can display them individually but not together. I have tried following the approach in the below link but I have been unable to get it to work:
Display Button With Text & Image In Xamarin IOS
This is how the button needs to look:
In Xamarin iOS I am using the below code to set the image and lay out the controls in the button:
DeviceMenuItems m_menuItem = HHMenuContainer.Instance.GetMenuItems().DeviceMenuItems.SingleOrDefault(l => l.LineNo.ToString() == m_lineNo);
if (m_menuItem != null && m_menuItem.Base64Picture != null)
{
byte[] bytes = Convert.FromBase64String(m_menuItem.Base64Picture);
NSData data = NSData.FromArray(bytes);
UIImage uiimage = UIImage.LoadFromData(data);
//this line is not relevant to the problem
uiimage = ChangeImageBackground(uiimage);
if (uiimage != null)
{
//m_control is the UIButton
//commenting this out removes the image and shows the text
m_control.SetImage(uiimage, UIControlState.Normal);
float titleLabelHeight = (float)(m_control.TitleLabel.Frame.Size.Height + 10);
m_control.ImageEdgeInsets = new UIEdgeInsets(-(titleLabelHeight), 0, titleLabelHeight, 0 );
float imageHeight = (float)(m_control.ImageView.Frame.Size.Height * 0.5);
imageHeight -= titleLabelHeight;
m_control.TitleEdgeInsets = new UIEdgeInsets(imageHeight, 0, -(imageHeight), 0);
}
}
With the above code I get the following button (without the required text):
If I comment the ‘SetImage’ line out I get the following button:
Does anyone have any ideas why the above approach isn’t working?
From the shared link , it show left title and right image.If want up and down layout, you can try this:
button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, -button.ImageView.Frame.Size.Height - 5,0);
button.ImageEdgeInsets = new UIEdgeInsets( -button.TitleLabel.Frame.Size.Height - 5, 0 , 0, -button.TitleLabel.Frame.Size.Width);
Note: If the button's frame is set and the width of the button is not enough to display the size of the image and text at the same time, the size of the titleLabel will get an error. So if you need to set the frame, it is recommended to set the width of the button to frame.size.width * 2, and then set the frame after the titleEdgeInsets and imageEdgeInsets are all set.
Despite trying various methods posted on SO, I was unable to have the simulator display tab bar icons when initialising the tab bar controller.
For each icon, I provided 3 sizes i.e. 25px by 25px, 50px by 50px (#2x) and 75px by 75px (#3x).
This is how my Tabbar is showing up in the simulator
Below are the Tab and Image attributes respectively
Here is one of my icon at 75px x 75px (#3x)
Can someone please guide me where I am going wrong ?
For Swift 3.0
Set programmatically for selected Tab and un-selected Tab Images
let arrayOfImageNameForSelectedState = ["tabBar_img_1", "tabBar_img_2", "tabBar_img_3"]
let arrayOfImageNameForUnselectedState = ["tabBar_img_1", "tabBar_img_2", "tabBar_img_3"]
if let count = self.tabBar.items?.count {
for i in 0...(count-1) {
let imageNameForSelectedState = arrayOfImageNameForSelectedState[i]
let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
self.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
}
}
I finally figured out that the problem was to do with the xcassets folder. To solve this I created a new xcassets folder in Xcode.
File > New > Resource > Asset Catalog.
As shown below I made sure my target application is selected as well.
I dragged and dropped the images from the old xcassets folder to the new one and all the images were loading.
Try modify like this:-
And then update your custom image programmatically
if let tabBarItems = yourTabBar?.items {
tabBarItems[0].selectedImage(UIImage(name: yourImage)
}
I am making an iOS app where the user is supposed to be able to overlay one image over another. The user can choose a background image, which would be stored in a UIImageView called imageView. The user can then decided on a image to be overlaid on top of that image, which will be stored in a UIImageView called imageTwo. Then, the user is allowed to move and resize imageTwo. However, I want to then be able to merge those image views into one image and download it.
I am currently using the function below to try to download the image:
#IBAction func downloadImage(_ sender: Any) {
let bottomImage:UIImage = imageView.image!
let topImage:UIImage = imageTwo.image!
let mySize = CGSize(width: (imageView.frame.size.width), height:(imageView.frame.size.height))
UIGraphicsBeginImageContextWithOptions(mySize, false, 1)
bottomImage.draw(in: CGRect(x: 0,y:0,width: mySize.width,height: mySize.height))
topImage.draw(in: CGRect(x: imageTwo.frame.origin.x ,y: imageTwo.frame.origin.y , width: (imageTwo.frame.size.width),height:(imageTwo.frame.size.height)), blendMode:CGBlendMode.normal, alpha:1.0)
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIImageWriteToSavedPhotosAlbum(newImage, nil,nil,nil)
UIGraphicsEndImageContext()
}
However, it doesn't work very well.
This is how the image looks in my app:
Image viewed from app
However, when I download the image using my function from above, the background image looks stretched and the topImage/bird isn't in the right position: Downloaded image
Anyone have any suggestions for easier ways to accomplish this task or how I should change my code from above? I read someone on here suggested putting the UIImageViews into their own View, but that proved to be quite tricky as well...
The issue is that you are compositing the images that back your image views which are different from what is actually displayed by your imageviews. You can either apply the necessary transform so that the image matches what's show in the imageview or you can be lazy and just capture the image that's on the screen snapshotView(afterScreenUpdates:)
I load an local image (tif) on to the UIWebView:
string fileName = "image.tif";
string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
webView.ScalesPageToFit = true;
The image is bigger than the UIWebView.
Now I want that the image fits in the web view. How can I do this?
i am unable to give comment due to too less comment as i am a new user.
Plz goto nib file and check "Sceling" as "Scale page to fit"
Hope it will work
I'm not sure if this applies directly to a picture, but I had issues trying to get a webview to show correctly on the screen. This is the code that I used and hopefully it can work for you.
var webview = new UIWebView(this.View.Frame);
webview.LoadRequest(new NSUrlRequest(new NSUrl(myCustomUrl)));
this.NavigationController.PushViewController(
new UIViewController() { webview }, true);
Maybe setting the frame will help?
I have read through all of the solutions on here and still am having this issue. I have tried almost anything that I can think of or find.
We just updated to using Xcode 5.0.2 from Xcode 4.6.3 and the code from 4.6.3 worked with no issue. Currently my code looks like this. This is what used to work and I have come back to this because nothing else has worked either.
At this point the size is correct but its in the upper right corner of the screen.
I've also tried adding
actionViewController.View.Center = this.View.Center;
but it didn't change the centre. Any help is greatly appreciated. I'm going crazy at the moment :)
IStoryboard storyBoard = UIStoryboard.FromName ("WorkOrderActions_iPad", null);
VZWOActionViewController actionViewController = (VZWOActionViewController)storyBoard.InstantiateViewController ("VZWOActionViewController");
actionViewController.ModalPresentationStyle = UIModalPresentationStyle.PageSheet;
actionViewController.actionViewControllerID = actionViewControllerName;
this.PresentViewController (actionViewController, true, completionHandler: null);
if (actionViewController.View.Superview != null) {
actionViewController.View.Superview.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
actionViewController.View.Superview.Bounds = new RectangleF (0.0f,0.0f, 924.0f, 648.0f);
}
There is an issue in iOS 7, this can be fix only if you set transition to cross dissolve.
actionViewController.ModalTransitionStyle= UIModalTransitionStyleCrossDisolve