Releasing UIImage after use - uiimageview

I want to learn the right way to alloc an uiimage and release it for memory management.
When i use the following code it crashes when deallocating,
if i dont use [imager release];
program doesnt crash but it shows a leak for imager. Any ideas for solution?
UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:s#"nameofimage" ofType:#"jpg"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];
[imageView release];
[imager release];

The right way of doing memory management is as you have done.
UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"nameofimage" ofType:#"jpg"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];
[imager release];
[imageView release];
May be it work if you first release the uiimage than uiimageview
I am not finding any mistake in this code. If the application crashes then check if you are using the same object reference anywhere else after it gets released.

Related

Imageview And/OR Sound having memory Issue (going in back ground)

My Application is in ARC.
In any view of my application there is one back ground image and on that image some animations are continuously repeating the images.
Problem is : My application is going in background after some time.
I seen and tried Lots of thing on stack, google and on apple doc but didn't got satisfactory solution.
I have Three Question.
(1) How to Stop this ?
(2) Is there any other method to release the memory manually in ARC.
(3) If i use [self.view removeFromSuperview]; for UIView then also some memory didn't released in ARC ?
MyCode for Animation is
imageview = [[UIImageView alloc] init];
imageview.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy08" ofType:#"png"]];
[self.view addSubview:imgBubbleAnimation];
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy01" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy02" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy03" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy04" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy05" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy06" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy07" ofType:#"png"]],
nil];
[imageview setAnimationRepeatCount:0]; // 0 for continuous
imageview.animationDuration =3.0;
[imageview startAnimating];
And Sound files are (played when particular button clicked)
URL = [NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"ect1a" ofType:#"mp3"]];
audioSound = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil];
audioSound.delegate = self;
audioSound.volume = 5.0;
audioSound.numberOfLoops = 0;
[audioSound play];
I have 15 small sound files for one view and 15 animations for one view.
In every particular Animation there are min 15 images.
Any help, suggestion, link or tutorial are welcome.
Not using imageview.animationImages is the first thing you need to do to get your memory usage under control. That API should never be used as it lets you shoot yourself in the foot easily. See imageview-animation-getting-more-memory or unpredictable-crash-due-to-uiview-animations or iphone-app-memory-leak-with-uiimage-animation-problem-testing-on-device.

iOS UIImageView animationImages display remote pictures

I have three urls just like "http://xxx.x.xx.xx:8180/admin/upload/advert/advert_img.jpg",
they are pictures in the remote server.
And I have an UIImageView, I want to use:
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 504.0, 320.0, 40.0)];
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"advert_img"],[UIImage imageNamed:#"img01"],[UIImage imageNamed:#"img02"], nil];
_imageView.animationImages = images;
_imageView.animationDuration = 15;
[_imageView startAnimating];
to display these pictures. How can I do this? Thank you very much.
Try this :
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"advert_img.png"],
[UIImage imageWithName:#"img01.png"],
[UIImage imageWithName:#"img02.png"],
nil];
UIImageView *animatedIMvw = [[UIImageView alloc] init];
animatedIMvw.animationImages = frames;
[animatedIMvw startAnimating];

xcode: segmented control appears on sim, not on device

I have an iPad app with several segmented controls, including one view with two controls.
On that view, everything shows in the sim, both normal and retina. However, in the device, only one shows.
Below is the code that does not show on the device.. I have checked and all the images that make up the control are copied in the bundle resources. I've tried removing, cleaning, etc. No joy. I must be missing something (hopefully simple)..
UISegmentedControl *controls = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
nil]];
CGRect frame = CGRectMake(35, 70, 700, 35);
controls.frame = frame;
}
[controls addTarget:self action:#selector(drawSegmentAction:) forControlEvents:UIControlEventValueChanged];
controls.segmentedControlStyle = UISegmentedControlStyleBar;
controls.momentary = YES;
controls.tintColor = [UIColor grayColor];
[self.view addSubview:controls];
}
For reference, this code in the same view does work:
For reference, this control code does work:
-(void) buildColorBar {
//NSLog(#"%s", __FUNCTION__);
UISegmentedControl *colorControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects: [UIImage imageNamed:#"White.png"],
[UIImage imageNamed:#"Red.png"],
[UIImage imageNamed:#"Yellow.png"],
[UIImage imageNamed:#"Green.png"],
[UIImage imageNamed:#"Blue.png"],
[UIImage imageNamed:#"Purple.png"],
[UIImage imageNamed:#"Black.png"],
nil]];
NSLog(#"Portrait");
CGRect frame = CGRectMake(35, 950, 700, 35);
colorControl.frame = frame;
// When the user chooses a color, the method changeBrushColor: is called.
[colorControl addTarget:self action:#selector(changeBrushColor:) forControlEvents:UIControlEventValueChanged];
colorControl.segmentedControlStyle = UISegmentedControlStyleBar;
// Make sure the color of the color complements the black background
colorControl.tintColor = [UIColor grayColor];
// Add the control to the window
[self.view addSubview:colorControl];
}
Is there a rule against using two segmented controls in one view?
I found that one image in the segmented control - while it looked like it was in the bundle - it wasn't. PITA, but at least, it works..

UITapGestureReconizer error

I am getting errors with my UITapGestureReconizer like "UITapGestureReconizer undeclared". Because UITapGestureReconizer is inside a concrete class should it not be declared? and how do I fix it... below is where it first shows up in the code.
- (void)loadView {
[super loadView];
// set the tag for the image view
[imageView setTag:ZOOM_VIEW_TAG];
// add gesture recognizers to the image view
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[imageView addGestureRecognizer:doubleTap];
[doubleTap release];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerTap:)];
[imageView addGestureRecognizer:twoFingerTap];
[twoFingerTap release];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
}
UITapGestureRecognizer is a part of UIKit, so it should be declared as long as you’re importing <UIKit/UIKit.h> somewhere. One problem I see in your code, though, is this:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[imageView addGestureRecognizer:doubleTap];
[doubleTap release];
…
[doubleTap setNumberOfTapsRequired:2];
At the point where you call [doubleTap setNumberOfTapsRequired:2], you don’t have a valid reference to doubleTap. It’s most likely retained by imageView, but that’s an implementation detail that you can’t rely on. Move the call to -setNumberOfTapsRequired: before the call to -release to ensure that this doesn’t break in a future version of iOS.

change uibarbuttonitem background using image

i have an uibarbuttonitem, but i was initialize it using initwithcustomview. I want to change it backgrounds using an image, but i dont know how to do. I was using setBackground method, like this
NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:#"bunga.jpg"];
_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];
but the uibarbuttonitem still not show the image, it's just change the segmented control background, not the barbutton.
Can somebody help me?
Perhaps you want to change the tint-color (#property(nonatomic, retain) UIColor *tintColor) because UISegmentedControl has no background-color (just because it inherits it from UIView does not mean it uses it though)

Resources