Remove programmatically added UIImageView
You can keep track of that imageView
in some property, and when you want to remove it you simply:
imageView.removeFromSuperview() // this removes it from your view hierarchy
imageView = nil; // if your reference to it was a strong reference, make sure to `nil` that strong reference
BTW, as you may know, UIImage(named:...)
will cache the images in memory. If you want that, fine, but if not, you might want to use UIImage(contentsOfFile:...)
with the fully qualified path to that resource. If you use UIImage(named:...)
, the image will stay in memory even after the UIImageView
has been removed. As the documentation says:
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using
imageWithContentsOfFile:
. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
Removing UIImageView added programmatically
Check that both the addSubview and removeFromSuperView are called in main thread
Check that startShowingImage is only called once, otherwise you have orphaned UIImageViews that you can never remove
- (void)startShowingImage
{
if (self.advertiseView) {
self.advertiseView = [[UIImageView alloc] initWithFrame:CGRectMake(80,320,150,97)];
self.advertiseView.image = [UIImage imageNamed:@"blocksAd.png"];
[self.view addSubview:self.advertiseView];
NSLog(@"startShowing");
}
}
Use property syntax unless you specifically need otherwise to avoid problems
iOS - adding/removing a subview programmatically
If you are not going to use the image again, there is no need to keep a pointer to it. Further, if you use IBOutlet
, you need to add the view in IB as well. In this specific example I would say option 3 makes the most sence, especially considering that with this choice you can began with a standard "view based application" template and just add the bits about the image view and leave the rest alone. One last observation of choice 3 though; the 2 messages to window;
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
Appear to be outside the scope of any method. This is likely just a copy and paste error, but make note that they should located within "didFinishLaunchingWithOptions
:"
Removing image from UIImageView
Setting the UIImageView
as:
myImageView.image = nil
is the correct way to clear an UIImageView
. Are you loading the image when your calling function returns? Is your UIImageView
being declared, and/or used elsewhere in your main function?
How to remove UIView from the superView with specific position programmatically
You could use tag
for each UIImageView
based on the indexPath.row
so you could refer by tag.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let image = UIImage(data: Saveddata[indexPath.row].photo as! Data)
//create image view programmatically
let imageView = UIImageView(frame: CGRect(x: 130 + (i * 10), y: 50 + (i * 5), width: 50, height: 100))
imageView.contentMode = .scaleAspectFit
imageView.tag = 100 + indexPath.row
imageView.image = image
view.addSubview(imageView as UIView)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// get imageView by its tag
let deselectedImageView = view.viewWithTag(100 + indexPath.row) as! UIImageView
deselectedImageView.removeFromSuperview()
}
Delete images programmatically by accessing their tag
You should use viewWithTag
to retreive the last tag, remove that tag from the array and remove the image from its superview.
if lstPoint.count != 0,
let lastTag = lstTagImage.last {
lstPoint.remove(at: lstPoint.count - 1)
lstTagImage.removeLast()
let imageView = view.viewWithTag(lastTag)
imageView.removeFromSuperview()
}
How to Programmatically Remove A UIImageView By touchGesture
A tap gesture recognizer has a view property that points to the view it's attached to. So, in the action method for the recognizer, use it to remove itself from its superview,
-(void) tapped:(UITapGestureRecognizer *)recognizer{
[recognizer.view removeFromSuperview];
}
It's hard to tell why your method isn't working. It could be because _ZombieView is nil (I don't see anywhere you actually create _ZombieView as opposed to ZombieView which you do create). BTW, you should start your property and variable names with a lowercase letter to conform to the usual objective-c naming conventions.
Related Topics
Removing a Closure from an Array
Programmatically Create an Nsviewcontroller Without an Xib in Swift 3
Refreshing Auth Token with Moya
Weak Method Argument Semantics
Swift Conditional Conformances with Generic Type
Is This Safe to Call Wait() of Dispatchsemaphore Several Times at One Time
iOS Swift Didbegincontact Not Being Called
Uisearchbar's Set_Cancelbuttontext: Ivar Is Prohibited
How to Initialise a New Nsdocument Instance in Swift
Using @Fetchrequest(Entity: ) for Swiftui MACos App Crashes
Closures Return Value (Previously Completionblock)
App Crashes from IPA File But Runs Fine from Xcode
How to Subclass a Class Which Doesn't Have Any Designated Initializers
How to Add Custom Init for String Extension
How to Get the Kvc-String from Swift 4 Keypath
Make a Type Itself -- Not Its Instances -- Conform to a Protocol
Need Detailed Explanation for Memoize Implementation in Swift (Wwdc 14, Session 404)
Pop View Controller Using Screen Edge Pan Gesture Recogniser Not Following the Thumb