UIImage in array > how to remove it - ios

I have an array of UIImages that I show in UIViews like this
UIImage *image=[self.currentAlphabet objectAtIndex:i ];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)];
imageView.image=image;
[self.view addSubview:imageView];
now later I want to remove these images from the view. I thought it should work like this
UIImage *image=[self.currentAlphabet objectAtIndex:i ];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
[imageView removeFromSuperview];
but it doesn't work like that... do I need to save the UIImageViews in the array or is there any solution that needs less changes in the code written already?

You are creating a brand new view object (which isn't in the hierarchy) and then removing it from the view hierarchy. It doesn't work for this reason.
You have to remove the view object that you've previously added to view hierarchy (in the first snippet).
Basically, you have to keep track of those views: store them also in an NSMutableArray, use the tagproperty of UIView or use an instance variable.
It depends by how many views you have to add to your hierarchy. If you have a few of them, an instance variable is fine. If you have many of them, use an NSMutableArray.
For example, using a secondary NSMutableArray:
UIImage *image = [self.currentAlphabet objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)];
imageView.image = image;
[self.view addSubview:imageView];
[imageViewArray addObject:imageView];
later on (I suppose that at this point you know the index of the object to remove):
[imageViewArray removeObjectAtIndex:i]

As long as the only image views in your view are the ones you want removed, a simple method to remove them is the following:
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}

What you are doing is creating a new Instance of UIImageView and without even Adding it to your view heirarchy you are removing it.
UIImage *image=[self.currentAlphabet objectAtIndex:i ];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];// It's a fresh new ImageView
[imageView removeFromSuperview];
What you can do is...
1) If there is only one ImageView in your View.. Then Create a Class Variable in .h File for UIImageView..
UIImageView *yourImageView; // in .h file
yourImageView.image = //Set Image here.
yourImageView.image = nil; // For removing the image.
2) Or If you don't want to create a Class variable for that.. Just give tag to your ImageVIew... And access it with viewWithTag method of UIView.
yourImageView.tag = 2;
and access it like following.
UIImageView *imageViewRef = [self.view viewWithTag:2];
[imageViewRef removeFromSuperView];
Hope this will help you..

Related

Releasing UIImageViews for memory

I will constantly call this method multiple times:
-(void)entityMaker {
UIImageView *entity;
NSUInteger image = [self getRandomNumberBetween:0 to:1];
NSUInteger xPosition = [self getRandomNumberBetween:20.0 to:self.width - 20.0];
entity = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, 20, 20, 20)];
[entity setImage:[self.imageArray objectAtIndex:image]];
[self.view addSubview:entity];
[self performSelector:#selector(animate:) withObject:entity afterDelay:2.0];
}
-(void)animate:(UIImageView *)image {
[image release];
//release has been deprecated
}
But, I don't want millions of image to stay on the screen.
How can I get rid of them (after any set number of seconds) so that they no longer take up memory and or no longer being displayed?
If you have no strong reference to the image view (which you don't in the code you posted), it will be deallocated after you remove it from its superview.
-(void)animate:(UIImageView *)image {
[image removeFromSuperview];
}
You could also use a property UIImageView and just update the image eveytime instead of creating a new UIImageView every-time and removing it.

snapshotViewAfterScreenUpdates creating a blank image

I'm trying to create some composite UIImage objects with this code:
someImageView.image = [ImageMaker coolImage];
ImageMaker:
- (UIImage*)coolImage {
UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coolImage"]]; //This is a valid image - can be viewed when debugger stops here
[composite addSubview:imgView];
UIView *snapshotView = [composite snapshotViewAfterScreenUpdates:YES];
//at this point snapshotView is just a blank image
UIImage *img = [self imageFromView:snapshotView];
return img;
}
- (UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
I just get back a blank black image. How can I fix?
Supplying YES for -snapshotViewAfterScreenUpdates: means it needs a trip back to the runloop to actually draw the image. If you supply NO, it will try immediately, but if your view is off screen or otherwise hasn't yet drawn to the screen, the snapshot will be empty.
To reliably get an image:
- (void)withCoolImage:(void (^)(UIImage *))block {
UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coolImage"]]; //This is a valid image - can be viewed when debugger stops here
[composite addSubview:imgView];
UIView *snapshotView = [composite snapshotViewAfterScreenUpdates:YES];
// give it a chance to update the screen…
dispatch_async(dispatch_get_main_queue(), ^
{
// … and now it'll be a valid snapshot in here
if(block)
{
block([self imageFromView:snapshotView]);
}
});
}
You would use it like this:
[someObject withCoolImage:^(UIImage *image){
[self doSomethingWithImage:image];
}];
The snapshotted view has to be drawn to the screen for a snapshot view to not be blank. In your case, the composite view must have a superview for drawing to work.
However, you should not be using the snapshotting API for this kind of action. It is very inefficient to create a view hierarchy for the sole purpose of creating an image. Instead, use the Core Graphics API's to setup a bitmap image context, perform drawing and get back the result using UIGraphicsGetImageFromCurrentImageContext().
The reason it's only rendering a black rectangle is because you're drawing the view hierarchy of the snapshot view, which is non-existent.
To make it work, you should pass composite as the parameter like so:
- (UIImage*)coolImage {
UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coolImage"]]
[composite addSubview:imgView];
UIImage *img = [self imageFromView:composite];
// Uncomment if you don't want composite to have imgView as its subview
// [imgView removeFromSuperview];
return img;
}

iOS UITableView setBackgroundView with 2 subviews

I'm running into a problem trying to perform a certain effect on iOS with tableviews...I know this can be done with normal UIViewControllers, but can't seem to pull it off with UITableViewControllers.
The effect is showing a clear background image behind a tableview and and as the user scrolls down the tableview, the background blurs. No problem getting that to happen.
The issue I'm having is that using a UITableViewController, I can only seem to have 1 ImageView behind the TableView and to create the blurring effect, the extension i am using will require 2 image views, one of which is the blurred image and has it's opacity set to 0 at launch.
This is code I know works using a UIViewController:
UIImage *background = [UIImage imageNamed:#"bg_004.png"];
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backgroundImageView];
self.blurredImageView = [[UIImageView alloc] init];
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredImageView.alpha = 0;
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.view addSubview:self.blurredImageView];
This is the code I am trying on the UITableViewController that does not work:
UIImage *background = [UIImage imageNamed:#"bg_004.png"];
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.tableView setBackgroundView:self.backgroundImageView];
// Also tried this, to no success
[self.tableView.backgroundView addSubview:self.blurredBackgroundView];
self.blurredBackgroundView = [[UIImageView alloc] init];
self.blurredBackgroundView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredBackgroundView.alpha = 0;
[self.blurredBackgroundView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.tableView setBackgroundView:self.blurredBackgroundView];
// Also tried this, to no success
[self.tableView.backgroundView addSubview:self.blurredBackgroundView];
If anyone has some suggestions how how to make this work, i'd love some advice. I really wouldn't like to change my code from a TableViewController to a UIViewController
Set the table view's backgroundView property to point to an instance of UIView, and then add your image views as subviews of the new view. You can't directly add subviews to instances of UIImageView

how to set background image properly in iOS

I am working on a view based app, I have set the background image as follows.
.h file:
UIImageView *BackgroundImage;
.m file:
viewdidLoad method:
BackgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BG.png"]];
[[self view] addSubview:BackgroundImage];
[BackgroundImage.superview sendSubviewToBack:BackgroundImage];
and on the rotate method:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
BackgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BG2.png"]];
[[self view] addSubview:BackgroundImage];
[BackgroundImage.superview sendSubviewToBack:BackgroundImage];
[super viewDidLoad];
}
Using this code I am able to set the background, but on rotation the first loaded image is not removed and the second one is loading just behind the first one, so just half of the image is visible, because of the first image is in front of second one.
Is there is a better way to do this?
OR
How can I just remove the first image and then set the second one?
Please help me to resolve this.
Change your didRotate... method to:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[BackgroundImage removeFromSuperview];
BackgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BG2.png"]];
[[self view] addSubview:BackgroundImage];
}
Notice the [BackgroundImage removeFromSuperview]; line. What we are doing here is whenever the device rotates, you remove the old image view and add another one.
Also please notice that variable names should start with lower case, so BackgroundImage should be backgroundImage. This is to tell them apart from classes' names.
If you change your code like this should to work
BackgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BG.png"]];
[self.view addSubview:BackgroundImage];
[self.view sendSubviewToBack:BackgroundImage];
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[BackgroundImage removeFromSuperview];
BackgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BG2.png"]];
[self.view addSubview:BackgroundImage];
[self.view sendSubviewToBack:BackgroundImage];
[super viewDidLoad];
}
This is because you are initializing the UIImageview again. Better to use same UIImageView that is backgroundImage with different image.
[Background Image set image:[UIImage imageNamed:#"BG2.png"]];
Just set the new image when rotating:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
BackgroundImage.image = [UIImage imageNamed:#"BG2.png"];
}
Your code kept adding new image views when rotating.
How [do stuff] properly in iOS?
Start your ivar names using an underscore and a lowercase letter: _backgroundImage instead of BackgroundImage.
Insert a subview in the right spot: [self.view insertSubview:_backgroundImage atIndex:0];

Clearing Buffer Memory in Xcode

I make a app which include a image view within scroll view.
I have 3 classes and each class add images into image view.
When i press button1, it call class1 and it fill Class1images(from image001 to image200).
When i press button2, it call class2 and it fill Class2images(from image201 to image400)
The problem is when i call class1, image view show class1images(from image001 to image200).
After calling class1, i call class2.
When i call class2, my image view can't show class2images(from image201 to image400)
But, class1Images (from image001 to image200) are remaining in app.
I think class1 images are still in memory, so that class2 images can't add to memory.
I want to remove class1images when i call class2.
When i call next class, it will remove old images in memory and replace with new images.
But, my app is develop with storyboard and ARC.
So, i can't release memory manually in ARC mode.
Is there any ways to remove old images in memory?
- (void)viewDidLoad{
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 200;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image002.jpg"];
:
:
UIImage *image200 = [UIImage imageNamed:#"Image200.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2, . . . ,image200,nil];
ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0,self.view.frame.size.width, self.view.frame.size.height)];
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews,self.view.frame.size.height);
[self.view addSubview:ScrollView];}
setting an imageview's animationImages property to a new array will remove the olf array and animate the new one.
to be 110% safe call stopAnimation, set the images, call startAnimation
I think your scrollview is not getting cleaned up. You need to remove previous images from scrollView and after that add images from next class.
try to implement something like this for removing images from scrollview and after this add other images on button click
for(UIView *subView in self.ScrollView.subviews)
{
if([subView isKindOfClass:[UIImageView class]])
[subView removeFromSuperview];
}
Hopefully can help you. Just give it a try.

Resources