I have the following implementation. Each view has animation, but my problem is that all animation run no matter visible or invisible.I only want the currently seen(active) view animation works, other view animation are disabled unless until user scrolls it.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
if (view == nil){
if(index==0)
{
view=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 600, 600)];
((UIImageView *)view).animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:#"Walking-1.png"],
[UIImage imageNamed:#"Walking-2.png"],
[UIImage imageNamed:#"Walking-3.png"],
[UIImage imageNamed:#"Walking-4.png"],
[UIImage imageNamed:#"Walking-5.png"],nil];
((UIImageView *)view).animationDuration = 1.5;
[((UIImageView *)view) startAnimating];
}
else if(index==1)
{
view=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 600, 600)];
((UIImageView *)view).animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:#"Biking-1.png"],
[UIImage imageNamed:#"Biking-2.png"],
[UIImage imageNamed:#"Biking-3.png"],
[UIImage imageNamed:#"Biking-4.png"],
[UIImage imageNamed:#"Biking-5.png"],nil];
((UIImageView *)view).animationDuration = 1.5;
[((UIImageView *)view) startAnimating];
}
view.contentMode = UIViewContentModeCenter;
[view.layer setMasksToBounds:YES];
}
return view;
}
iCarouselDelegate has a method carouselCurrentItemIndexDidChange you should do stop/start your animations there, but not in data source method. So save all your animations into array with an index equal to an item index and use currentItemIndex property of carousel in carouselCurrentItemIndexDidChange to start appropriate animation. You will need to store previous index somewhere in your class, to be able to stop previous animation, before start new one.
#import UIKit;
#interface CarouselController: UIViewController
#property(nonatomic, readonly) NSArray *carouselItems;
#property(nonatomic, assign) NSInteger lastItemIndex;
#end
#implementation CarouselController
- (instancetype)init {
self = [super init];
if (self) {
[self initializeCarouselContent];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initializeCarouselContent];
}
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self initializeCarouselContent];
}
return self;
}
- (void)initializeCarouselContent {
self.lastItemIndex = 0;
_carouselItems = #[[CarouselController activityAnimationView: #"Walking"], [CarouselController activityAnimationView: #"Biking"]];
UIImageView *currentAnimation = self.carouselItems[self.lastItemIndex];
[currentAnimation startAnimating];
}
+ (UIImageView *)activityAnimationView:(NSString *)activity {
UIImageView *result = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 600, 600)];
NSMutableArray *activityImages = [[NSMutableArray alloc] init];
for (int activityIndex = 1; activityIndex <= 5; activityIndex ++) {
NSString *imageName = [NSString stringWithFormat:#"%#-%i.png", activity, activityIndex];
[activityImages addObject:[UIImage imageNamed:imageName]];
}
result.animationImages = activityImages;
result.animationDuration = 1.5;
result.contentMode = UIViewContentModeCenter;
[result.layer setMasksToBounds:YES];
return result;
}
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return [_carouselItems count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *) view{
if (view == nil){
view = self.carouselItems[index];
}
return view;
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel {
if (carousel.currentItemIndex != self.lastItemIndex) {
UIImageView *currentAnimation = self.carouselItems[self.lastItemIndex];
[currentAnimation stopAnimating];
self.lastItemIndex = carousel.currentItemIndex;
currentAnimation = self.carouselItems[self.lastItemIndex];
[currentAnimation startAnimating];
}
}
#end
Related
I have a custom UIView with a UICollectionView.
On screen rotation I am trying to get the UICollectionView to stretch across the screen, and then redraw its cells.
After I had the data downloaded I tried both [grid setNeedsLayout] and [grid setNeedsDisplay] but that didn't work.
This is what I want to happen:
Portrait
Landscape
(This is also how it appears when the app is started in landscape, but if you change to portrait it doens't update.)
But this is what I get if I start in Portrait mode and switch to Landscape.
I am creating these views programmatically. I am not using any Storyboards.
I have tried:
-(void)viewDidLayoutSubviews {
grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
}
I have also tried toying with:
- (void) viewWillLayoutSubviews {
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
//LANDSCAPE
if(grid){
NSLog(#"Grid Needs Landscape Layout");
grid.frame =CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
[grid refreshData];
}
}else {
//PORTRIAT
if(grid){
NSLog(#"Grid Needs Portrait Layout");
grid.frame =CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
[grid refreshData];
}
}
}
But I can't get it to stretch.
Any help?
MyThumbnailGridView
#interface ViewController () <UINavigationControllerDelegate> {
MyThumbnailGridView *grid;
NSMutableArray * arrImages;
}
- (void)viewDidLoad {
[super viewDidLoad];
arrImages = [NSMutableArray new];
grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
//grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectZero];
NSLog(#"showThumbnailGrid Grid View Size: %#", NSStringFromCGRect(grid.frame));
[self.view addSubview:grid];
[self getListOfImages];
}
-(void) getListOfImages {
//Do background task to get images and fill arrImages
[self onDownloadImageDataComplete];
}
- (void) onDownloadImageDataComplete{
grid.imageDataSource = arrImages;
// [grid setNeedsLayout];
// [grid setNeedsDisplay];
}
//...
#end
*MyThumbnailGridView.h
#interface MyThumbnailGridView : UIView
-(id)initWithFrame:(CGRect)frame;
-(void) refreshData;
#property (nonatomic,strong) NSArray *imageDataSource;
#end
*MyThumbnailGridView.m
#interface MyThumbnailGridView () <UICollectionViewDelegate, UICollectionViewDataSource>{
UICollectionView *collectionView;
}
#end
#implementation MyThumbnailGridView
- (instancetype) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
[self customInit];
}
return self;
}
- (void) customInit {
collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[[MyFlowLayout alloc] init]];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.allowsMultipleSelection = NO;
collectionView.showsVerticalScrollIndicator = YES;
[collectionView setBackgroundColor:[UIColor darkGrayColor]];
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:#"MyId"];
[self addSubview:collectionView];
}
- (void) refreshData {
NSLog(#"Refresh Grid Data");
[collectionView reloadData];
}
////other code
#end
MyFlowLayout
#interface MyFlowLayout : UICollectionViewFlowLayout
#end
#implementation MyFlowLayout
- (instancetype)init{
self = [super init];
if (self)
{
self.minimumLineSpacing = 1.0;
self.minimumInteritemSpacing = 1.0;
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return self;
}
- (CGSize)itemSize {
NSInteger numberOfColumns = 3;
CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
return CGSizeMake(itemWidth, itemWidth);
}
#end
MyCollectionViewCell
#interface MyCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) UIImageView *imageView;
#end
#implementation MyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [UIImageView new];
[self.imageView setContentMode:UIViewContentModeScaleAspectFill];
[self.imageView setClipsToBounds:YES];
[self.imageView setBackgroundColor:[UIColor darkGrayColor]];
[self.contentView addSubview:self.imageView];
}
return self;
}
- (void)prepareForReuse {
[super prepareForReuse];
self.imageView.image = nil;
[self.imageView setHidden:NO];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self.imageView setFrame:self.contentView.bounds];
}
#end
This can be solved with
either you can change the frame of MyThumbnailGridView view in delegate function of orientation or create the view with constraints like this
(void)viewDidLoad {
[super viewDidLoad];
arrImages = [NSMutableArray new];
grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
[self.view addSubview:grid];
[self getListOfImages];
}
-(void)viewDidLayoutSubviews
{
if(Once){
Once = NO;
// adding constraints
MyThumbnailGridView.translatesAutoresizingMaskIntoConstraints = NO;
[self.MyThumbnailGridView.widthAnchor constraintEqualToConstant:self.view.frame.size.width].active = YES;
[self.MyThumbnailGridView.heightAnchor constraintEqualToConstant:self.view.frame.size.height/2].active = YES;
[self.MyThumbnailGridView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[self.MyThumbnailGridView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:self.view.frame.size.height/2].active = YES;
}
}
Also implement this
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[self.view layoutIfNeeded];
[MyThumbnailGridView.collectionView invalidate];
// Do view manipulation here.
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
The problem is that the collection view itself is not changing size when the app rotates. You have given the collection view a fixed frame and then just walked away. So it never changes. So never lays itself out again.
You should give your MyThumbnailGridView and its collection view subview autolayout contraints to their superviews, so that they change size correctly when the app rotates.
I am attempting to create a view to show a list of items and prices using two UILabels in a UIView.
In my UIViewController I call my subview LineItemView and pass data, and return the UIView to the main view that will hold the subviews.
Though my view is returning empty. The strings are null.
ViewController.m
#import "LineItemView.h"
//...
#property (weak, nonatomic) IBOutlet UIView *viewCharges;
//...
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
}
- (void) loadData {
//Here we will fill in the viewCharges view
LineItemView * view = [[LineItemView alloc]init];
view.linePrice = #"1.00";
view.lineItem = #"Something";
[self.viewCharges addSubview:view];
}
LineItemView.h
#interface LineItemView : UIView {
UILabel * lblLineItem, *lblLinePrice;
}
#property (nonatomic,strong) NSString* lineItem;
#property (nonatomic,strong) NSString* linePrice;
LineItemView.m
#define LABEL_MINIMUM_HEIGHT 32
#define VERTICAL_MARGIN 5
#define HORIZONTAL_MARGIN 10
#implementation LineItemView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self createUI];
[self updateData];
NSLog(#"\n\n\n Line Item: %# Price: %# \n\n\n", self.lineItem, self.linePrice);
}
return self;
}
-(void) createUI {
lblLineItem = [[UILabel alloc] initWithFrame:CGRectZero];
lblLineItem.backgroundColor = [UIColor greenColor];
[self addSubview:lblLineItem];
lblLinePrice = [[UILabel alloc] initWithFrame:CGRectZero];
lblLinePrice.backgroundColor = [UIColor yellowColor];
[self addSubview:lblLinePrice];
}
- (void) updateLayout {
lblLineItem.frame = CGRectMake(HORIZONTAL_MARGIN, VERTICAL_MARGIN, 300, 35);
lblLinePrice.frame = CGRectMake(lblLineItem.frame.origin.x + lblLineItem.frame.size.width + HORIZONTAL_MARGIN, VERTICAL_MARGIN, 80, 35);
}
- (void) updateData {
lblLineItem.text = self.lineItem;
lblLinePrice.text = [NSString stringWithFormat:#"%0.2f", [self.linePrice floatValue]];
}
- (void) layoutSubviews {
[super layoutSubviews];
[self updateLayout];
}
What am I doing wrong?
If I want to continue calling LineItemView, how do I ensure that it is added below the previous one and ensure the viewCharges size is readjusted to fit all the subviews?
Solution using setters:
#implementation LineItemView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self createUI];
}
return self;
}
-(void) createUI {
lblLineItem = [[UILabel alloc] initWithFrame:CGRectZero];
lblLineItem.backgroundColor = [UIColor greenColor];
[self addSubview:lblLineItem];
lblLinePrice = [[UILabel alloc] initWithFrame:CGRectZero];
lblLinePrice.backgroundColor = [UIColor yellowColor];
[self addSubview:lblLinePrice];
}
- (void) updateLayout {
lblLineItem.frame = CGRectMake(HORIZONTAL_MARGIN, VERTICAL_MARGIN, 300, 35);
lblLinePrice.frame = CGRectMake(lblLineItem.frame.origin.x + lblLineItem.frame.size.width + HORIZONTAL_MARGIN, VERTICAL_MARGIN, 80, 35);
}
- (void) layoutSubviews {
[super layoutSubviews];
[self updateLayout];
}
- (void)setLineItem:(NSSTring *)lineItem {
_lineItem = lineItem;
lblLineItem.text = self.lineItem;
}
- (void)setLinePrice:(NSNumber *)linePrice {
_linePrice = linePrice;
lblLinePrice.text = [NSString stringWithFormat:#"%0.2f", [self.linePrice floatValue]];
}
I need add all colors of my views to NSMuttableArray. A try do it, but adding only one color of last view. My Array named colorArray. Please help me.
I think it should be done in a loop, but I do not know exactly where its cause and how to describe it
It's my ViewController
#import "ViewController.h"
#import "RandomView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize kol;
#synthesize sdvig1;
#synthesize sdvig2;
#synthesize myTextField;
#synthesize randView;
#synthesize redButton, greenButton, blueButton;
#synthesize colorArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
kol=10;
}
return self;
}
-(void) viewWillAppear:(BOOL)animated
{
myTextField.delegate = self;
kol=[[myTextField text] intValue];
}
- (void)viewDidLoad
{
[super viewDidLoad];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width/4, 30)];
myTextField.backgroundColor=[UIColor yellowColor];
[myTextField setKeyboardType:UIKeyboardTypeNumberPad];
[self.view addSubview:myTextField];
redButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, 20, self.view.frame.size.width/4, 30)];
redButton.backgroundColor=[UIColor redColor];
[redButton addTarget: self
action: #selector(redSort:) forControlEvents: UIControlEventTouchUpInside];
greenButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*2, 20, self.view.frame.size.width/4, 30)];
greenButton.backgroundColor=[UIColor greenColor];
[greenButton addTarget: self
action: #selector(greenSort:) forControlEvents: UIControlEventTouchUpInside];
blueButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*3, 20, self.view.frame.size.width/4, 30)];
blueButton.backgroundColor=[UIColor blueColor];
[blueButton addTarget: self
action: #selector(blueSort:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:redButton];
[self.view addSubview:greenButton];
[self.view addSubview:blueButton];
[self createView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark- Создание и удаление View's
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[myTextField resignFirstResponder];
[self textFieldShouldReturn:myTextField];
myTextField.text = #"";
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([self.myTextField.text isEqualToString:#""]) {
} else
{
[self.myTextField resignFirstResponder];
kol=[[self.myTextField text] intValue];
[self removeView];
[self createView];
}
return YES;
}
-(void)createView{
colorArray= [[NSMuttableArray alloc] init];
sdvig1=self.view.frame.size.width/(2*(kol+5));
sdvig2=self.view.frame.size.height/(2*(kol+5));
randView = [[RandomView alloc] initWithFrame:CGRectMake(0,self.myTextField.frame.origin.y+self.myTextField.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-(self.myTextField.frame.origin.y+self.myTextField.frame.size.height)) count:kol sdvig:CGPointMake(sdvig1, sdvig2) vc:self];
[colorArray addObject:randView.backgroundColor]; //IT'S MY ARRAY
NSLog(#"colorArray= %#", colorArray);
[self.view addSubview:randView];
}
-(void) removeView{
[randView removeFromSuperview];
}
#pragma mark- Цвета
-(UIColor *) randomColor
{
CGFloat red = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
CGFloat green = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
#end
And my View Class
//
// RandomView.m
// FewView
//
// Created by admin on 3/11/14.
// Copyright (c) 2014 admin. All rights reserved.
//
#import "RandomView.h"
#import "ViewController.h"
//#import <QuartzCore/QuartzCore.h>
#implementation RandomView
#synthesize randView;
//#synthesize myArray;
//#synthesize colorArrayRed, colorArrayBlue, colorArrayGreen, colorArray;
//#synthesize colorArray;
//CGFloat redBack, greenBack, blueBack, alphaBack;
- (id)initWithFrame:(CGRect)frame count:(NSInteger)kol sdvig:(CGPoint)sdvig vc:(ViewController*) delegat
{
self = [super initWithFrame:frame];
if (self)
{
// myArray = [[NSMutableArray alloc]init];
if (kol>0) {
[self setBackgroundColor:[delegat randomColor]];
randView = [[RandomView alloc] initWithFrame:CGRectMake(sdvig.x, sdvig.y, self. frame.size.width-2*sdvig.x, self.frame.size.height-2*sdvig.y) count:--kol sdvig:CGPointMake(sdvig.x, sdvig.y) vc:delegat];
self.layer.cornerRadius = 25;
self.layer.masksToBounds = YES;
[self addSubview:randView];
}
}
return self;
}
#end
Every time you call createView you're creating a new array. Try moving the colorArray = [[NSMutableArray alloc] init]; line into your viewDidLoad method.
I have a subclassed UITextView for placeholder and background image.
Here's the code
BKCUTextView.h
#import <UIKit/UIKit.h>
#interface BKCUTextView : UITextView
- (void)setPlaceholder:(NSString *)placeholder;
- (void)setPlaceholderColor:(UIColor *)placeholderColor;
- (void)setBackgroundImage:(UIImage *)img;
#end
BKCUTextView.m
#import "BKCUTextView.h"
#interface BKCUTextView()
{
NSString * _placeholder;
UIColor * _normalColor;
UIColor * _placeholderColor;
BOOL _needToShowPlaceholder;
UIImageView * _bgImageView;
}
#end
#implementation BKCUTextView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self _initEvents];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
if(_needToShowPlaceholder) { [_placeholder drawAtPoint:CGPointMake(0, 0) withFont:self.font]; }
}
- (void)setTextColor:(UIColor *)textColor
{
_normalColor = textColor;
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_needToShowPlaceholder = YES;
_placeholder = placeholder;
}
-(void)setBackgroundImage:(UIImage *)img
{
if(_bgImageView == nil)
{
_bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self addSubview:_bgImageView];
[self sendSubviewToBack:_bgImageView];
[self bringSubviewToFront:self];
}
[_bgImageView setImage:img];
}
- (void)_initEvents
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_textChanged:) name:UITextViewTextDidChangeNotification object:self];
}
-(void)_textChanged:(NSNotification *)noti
{
_needToShowPlaceholder = (self.text.length == 0);
if(_needToShowPlaceholder) { self.textColor = _placeholderColor; }
else { self.textColor = _normalColor; }
[self setNeedsDisplay];
}
instance
BKCUTextView * tv = [[BKCUTextView alloc] initWithFrame:CGRectMake(5, 55, 300, 50)];
[tv setBackgroundColor:[UIColor darkGrayColor]];
[tv setTextColor:[UIColor whiteColor]];
[tv setPlaceholderColor:[UIColor grayColor]];
[tv setPlaceholder:#"placeholder here!"];
[tv setBackgroundImage:[UIImage imageNamed:#"btn_coin_prs.png"]];
[self.view addSubview:tv];
[self.view bringSubviewToFront:tv];
each property - backgroundImage and placeholderText - works fine, but when I set them together, the placeholder is drawn under the backgroundImage.
How can I bring the text above the back image?
Try this method [self bringSubviewToFront:];
Hope it will help!
Every time I am entering the same view memory is increasing. Even I am releasing nothing is happening and I can't use ARC due to older app issue. Don't know how to solve this issue.
#import <UIKit/UIKit.h>
#import "CareServices.h"
#interface AttachedImage : UIViewController<ServerConnectionDelegate,UIScrollViewDelegate>
{
IBOutlet UIImageView *imageView;
CareServices *careServices;
UIScrollView *_scrollView;
UIView *activityView;
UIActivityIndicatorView *_activity;
}
#property (nonatomic,retain)UIImageView *imageView;
#end
#import "AttachedImage.h"
#import"XMLParserForOtherAttachments.h"
#import "ColorSchemes.h"
#implementation AttachedImage
#synthesize imageView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
activityView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2.5,self.view.frame.size.height/2.5, 60, 60)];
activityView.backgroundColor = [UIColor clearColor];
[self.view addSubview:activityView];
UIImageView *activityImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, activityView.frame.size.width, activityView.frame.size.height)];
activityImageView.image = [UIImage imageNamed:#"indicatorImage.jpeg"];
[activityView addSubview:activityImageView];
[activityImageView release];
_activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[_activity setFrame:CGRectMake(activityView.frame.size.width/5.8, activityView.frame.size.height/5.3, 40, 40)];
[activityView addSubview:_activity];
[_activity startAnimating];
[_activity release];
careServices = [CareServices currentInstance];
careServices.delegate = self;
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[super viewDidLoad];
self.navigationItem.title = #"Attachment View";
// self.view.backgroundColor = [ColorSchemes sharedInstance].primaryColor1;
self.view.backgroundColor = [UIColor blackColor];;
// Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated {
NSString *senderTagStr;
NSUserDefaults *senderD = [NSUserDefaults standardUserDefaults];
senderTagStr = [senderD valueForKey:#"sender tag"];
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_scrollView.maximumZoomScale = 4.0;
_scrollView.minimumZoomScale = 1.0;
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(400, 500);
if (careServices.loginMode == ELoginModeDemo) {
if ([senderTagStr isEqualToString:#"0"]) {
imageView.image = [UIImage imageNamed:#"R1.bmp"];
}
else {
imageView.image = [UIImage imageNamed:#"8.jpg"];
}
}
else {
NSString *str = [NSString stringWithString:[XMLParserForOtherAttachments sharedManager].fileURlStr];
NSString *trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSData *imgData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:trimmedStr]];
UIImage *myImage = [UIImage imageWithData:imgData];
imageView.image = myImage;
[imgData release];
}
[_scrollView addSubview:imageView];
[self.view addSubview:_scrollView];
[_activity stopAnimating];
[activityView removeFromSuperview];
}
- (void)viewDidUnload {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
self.imageView = nil;
[super viewDidUnload];
}
- (void)dealloc{
[imageView release];
[super dealloc];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
#end
Locating the offending allocations/implementations is quite easy once you learn to use Heapshot Analysis.
Heapshot Analysis allows you to take snapshots during your execution in order to detect growths over a period of time, which you can of course associate with user actions in many cases. So you could for example push a view controller onto the navigation stack in iOS, then step back and easily determine what the growth was in doing so. Then navigate to the implementations which caused the growths.