Remove uiimageview from view - swift

I have a feed and i am wanting to display images if the user does not have an image, it will say null in the json. if the user does have a image, the image will be set with the image url.
i want to remove it so it will not take up space in the tableview cell.
my code is:
if imageStringFromJson == nil {
//how do i remove the imageview? <-- need help here
} else {
//set image from url in imageview
}

Try:
imageView.removeFromSuperview()
Or:
imageView.hidden = true
Or animate:
UIView.animateWithDuration(1) {
imageView.alpha = 0
}
To animate back to showing, try this:
UIView.animateWithDuration(1) {
imageView.alpha = 1
}

Related

What control to use to capture image from camera and show on ui as thumbnail with events xamarin ios

I have created a view like this in my xamarin iOS mobile project.
User will be able to click on the capture images button to take a picture and set the image property of the image view. I would like to know how can i allow the user longpress a image (after it has been captured) and popup a message box to delete the image.
I have tried what Sameer has suggested in his comment like so in my ViewDidLoad
var gestureRecognizer = new UILongPressGestureRecognizer();
gestureRecognizer.AddTarget(() => ButtonLongPressed(gestureRecognizer));
img1.AddGestureRecognizer(gestureRecognizer);
img2.AddGestureRecognizer(gestureRecognizer);
When i click and hold on the image nothing happens. I have added these image views via the designer.
After a little more research and using the comment from #Junior Jiang - MSFT. I have made a bit progress but i would like to know which UIImage view has been clicked.
Heres my current code:
public JobImagesViewController(Job passedInCurrentJob) : base("JobImagesViewController", null)
{
currentJob = passedInCurrentJob;
uIImageViews = new List<UIImageView>();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
uIImageViews.Add(img1);
uIImageViews.Add(img2);
uIImageViews.Add(img3);
uIImageViews.Add(img4);
uIImageViews.Add(img5);
uIImageViews.Add(img6);
uIImageViews.Add(img7);
uIImageViews.Add(img8);
uIImageViews.Add(img9);
uIImageViews.Add(img10);
InitialiseImageGrid();
// Add a Save button to the navigation bar
this.NavigationItem.SetRightBarButtonItem(
new UIBarButtonItem("Save", UIBarButtonItemStyle.Done, (sender, args) =>
{
//TODO if else block with all logic to check if there are images etc.
//TODO prompt user and ask if they would like to save images.
UIAlertView alert = new UIAlertView("Save Images?", "Save images against the Job?", null, "Yes", new string[] { "No" });
alert.Clicked += (s, e) =>
{
if (e.ButtonIndex == 0) // Yes clicked
{
SaveJobImages();
}
};
alert.Show();
}), true);
}
[Export("ImageLongPressed:")]
public void ImageLongPressed(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State != UIGestureRecognizerState.Began)
{
return;
// Needed because selector is executed twice, because Long-press gestures are continuous
}
// Perform action of opening the dialog to select/take a picture, replacing the ? image with the new image
UIAlertView alert = new UIAlertView("Delete this image ?" , "Are you sure you want to delete this image?", null, "Yes", new string[] { "No" });
alert.Clicked += (s, e) =>
{
if (e.ButtonIndex == 0) // 'Accept' clicked
{
// TODO how to get the image which has been clicked??
}
};
alert.Show();
}
private void InitialiseImageGrid()
{
_imageList = DataAccess.GetImages(currentJob.jobAddressID);
var imageBytes = _imageList.Select(x => x.ImageBytes).ToList();
var gestureRecognizer = new UILongPressGestureRecognizer(this, new ObjCRuntime.Selector("ImageLongPressed:"));
gestureRecognizer.AddTarget(() => ImageLongPressed(gestureRecognizer));
// Populate the image views.
// TODO need to find a way to assign it to every imageview on the view without looping maybe linq???
int i = 0;
foreach (var item in imageBytes)
{
var imagedata = NSData.FromArray(item);
var img = UIImage.LoadFromData(imagedata);
if (uIImageViews != null && uIImageViews.Count > i)
{
uIImageViews[i].UserInteractionEnabled = true;
uIImageViews[i].AddGestureRecognizer(gestureRecognizer);
uIImageViews[i].AddGestureRecognizer(gestureRecognizer);
uIImageViews[i].Image = img;
}
i++;
}
It depends on how you are creating your view, are you using a XIB with all the images on there as buttons with images, and IBActions connected to those buttons. just make the sender a UILongPressGestureRecognizer.
If you are doing it through code, then in your ViewDidLoad you want to set your buttons to have an initial ? (or Image Needed) background image and then you add the UILongPress GestureRecognizer to each on of them. So if you had one button, you would do this:
public override void ViewDidLoad ()
{
// Perform any additional setup after loading the view
UIButton button = new UIButton (new System.Drawing.RectangleF(100, 100, 100, 30));
button.SetBackgroundImage ("imageNeeded", UIControlState.Normal);
var gestureRecognizer = new UILongPressGestureRecognizer ();
gestureRecognizer.AddTarget(() => this.ButtonLongPressed(gestureRecognizer));
button.AddGestureRecognizer(gestureRecognizer);
this.View.Add (button);
}
public void ButtonLongPressed(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State != UIGestureRecognizerState.Began)
{
return;
// Needed because selector is executed twice, because Long-press gestures are continuous
}
// Perform action of opening the dialog to select/take a picture, replacing the ? image with the new image
}
If want to use UILongPressGestureRecognizer in UIImage, should set ImageView's UserInteractionEnabled be true.
As follow:
List<UIImageView> imageViews = new List<UIImageView>();
UIImage image = UIImage.FromFile("off.png");
ImageViewOne.Image = image;
ImageViewOne.Tag = 1;
ImageViewTwo.Image = image;
ImageViewTwo.Tag = 2;
imageViews.Add(ImageViewOne);
imageViews.Add(ImageViewTwo);
foreach (var imageview in imageViews)
{
imageview.UserInteractionEnabled = true;
UILongPressGestureRecognizer uITapGestureRecognizer = new UILongPressGestureRecognizer();
uITapGestureRecognizer.AddTarget(() => { Console.WriteLine("You choose View " + imageview.Tag); });
imageview.AddGestureRecognizer(uITapGestureRecognizer);
}
Here is official document from Apple.

PDFAnnotation Radiobutton don't change background color or don't display value

I use PDFKit in iOS as PDF renderer and I have problem with background color in radio button.
I iterate all PDF annotations on all pages in the document.
override func viewDidAppear(_ animated: Bool) {
//let pdfData = ... received data from file storage ...
if let document = PDFDocument(data: pdfData) {
for i in 0...document.pageCount {
if let page = document.page(at: i) {
for annot in page.annotations {
if annot.widgetControlType == .radioButtonControl {
annot.backgroundColor = UIColor.red
}
}
}
}
}
}
When I set the background color, nothing happens.
But when I try to set the background color to the type of Text annotation, the color changes.
The difference between the Radio button and the Text annotation is in its type.
The Radio button has a widgetFieldType == .button and the Text has a widgetFieldType == .text.
I think this is the reason why it doesn't work.
Next I try to remove annotation, change his background color and add again. This also doesn't work.
if annot.widgetControlType == .radioButtonControl {
page.removeAnnotation(annot)
annot.backgroundColor = UIColor.red
page.addAnnotation(annot)
}
But when I create a new instance of PDFAnnotaion, and I add it to the page, it works.
if annot.widgetControlType == .radioButtonControl {
page.removeAnnotation(annot)
let newAnnot = PDFAnnotation(bounds: annot.bounds, forType: .widget, withProperties: nil)
newAnnot.backgroundColor = UIColor.red
newAnnot.widgetStringValue = annot.widgetStringValue
page.addAnnotation(newAnnot)
}
The big problem is that the value of this Radio button is not displayed, even though it contains the value itself.
If in another method I get all the values from PDF annotations, there is also the right value from this radio button.
The Radio button contains the value, only it is displayed.
I tried to copy all the properties PDF annotation from the annot to newAnnot, but it didn't help.
How to properly change the background color of the Radio button and display its value?

Can I put a static image in a UIButton.imageView? that is animating?

I have a custom UIButton (MovesButton) with an image that I don't want to change or remove. But when I set button.imageView?.animationImages with bunch of images and start animating it, it removes my button's initial photo I have. If I have the static photo as a background, it looks like it is on fire. I want the fire to be as a background and preferably in the same UIButton class.
private func putAnimation(button: MovesButton) {
var images: [UIImage] = []
if button.animation == ButtonAnimations.None {
print("None11")
return
} else if button.animation == ButtonAnimations.SmallFire {
for i in 0 ... ButtonAnimations.SmallFire.1 {
images.append(UIImage(named: "\(ButtonAnimations.SmallFire.0)\(i)")!) //ButtonAnimations.SmallFire.0 = "smallFire"
}
} else if button.animation == ButtonAnimations.BigFire {
for i in 0 ... ButtonAnimations.BigFire.1 {
images.append(UIImage(named: "\(ButtonAnimations.BigFire.0)\(i)")!)
}
} else { print("weird button animations") }
button.imageView?.animationImages = images
button.imageView?.animationDuration = 1
button.imageView?.startAnimating()
}
I don't want to use a hacky trick of putting an imageView or another button behind MovesButton
I want the same animation result as a button.imageView.animationImages but as the backgroundImage. Is this possible?
Instead of image view animation, use image animation. Call UIImage animatedImage(with:duration:) to form an animated image. Set that as your button’s background image and you’re all set.

Change UIBarButtonIcon in an if and else statement in Swift

Inside of an If and else statement I want to change the custom image of my BarButtonItem. How can I Do these by default I already have one custom Image but know I want that under some circumstances it changes to another custom Image.
If you have your images in Assets.xcassets you can do this:
if something {
yourBarButtonItem.image = imagename
} else {
yourBarButtonItem.image = anotherimagename
}
that should work. Else, if you have your image just in a folder inside your project you can to this:
if something {
yourBarButtonItem.image = UIImage(named: "imagename.extension")
} else {
yourBarButtonItem.image = UIImage(named: "imagename.extension")
}

NSView dataWithPDF inside rect but with background view

I wan´t to take a screenshot of a NSView but when I do this, I get an image without the background. All the subviews are show, but not the background. I use this code:
if let image = NSImage(data: background.dataWithPDF(inside: background.bounds)) {
let imageView = NSImageView(image: image)
imageView.imageScaling = .scaleProportionallyUpOrDown
return NSImageView(image: image)
}
I thought, ok when I get only the subviews, then I will make a screenshot of the superview and I thried the following:
if let superview = background.superview, let image = NSImage(data: superview.dataWithPDF(inside: background.frame)) {
let imageView = NSImageView(image: image)
imageView.imageScaling = .scaleProportionallyUpOrDown
return NSImageView(image: image)
}
But I get the same result. Even if I set the background color of my background view I don´t get an image without transparent background.
How can I resolve this?
thank you
Artur
I got an Answer:
background.lockFocus()
if let rep = NSBitmapImageRep(focusedViewRect: background.bounds){
let img = NSImage(size: background.bounds.size)
img.addRepresentation(rep)
return NSImageView(image: img)
}
background.unlockFocus()
You could have converted the views backing layer.backgroundColor to an image. Then assign the image to a backing image view that sits in the 0th index of your views heirarchy and use the method dataWithPDF -> that will successfully capture a background.