EXC_BREAKPOINT when going back to view with imgview in scrollview - ios

I call an view with an imgView inside a scrollview like this from another view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ImageViewController *view = [[self.menus objectAtIndex:[indexPath row]] objectForKey:#"VIEW"];
view.imgPath = [[self.menus objectAtIndex:[indexPath row]] objectForKey:#"PATH"];
[self presentViewController:view animated:YES completion:nil];
}
Then I show the image in the new view like this
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imgView;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:self.imgPath ofType:#"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
self.imgView = [[UIImageView alloc] initWithImage:image];
self.imgView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.scrollView addSubview:self.imgView];
self.scrollView.contentSize = image.size;
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
self.scrollView.maximumZoomScale = 1.0f;
self.scrollView.zoomScale = minScale;
[self centerScrollViewContents];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
}
- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imgView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imgView.frame = contentsFrame;
}
This all works with no problems and I can dismiss the view like this
- (IBAction)BackBtnPress:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
However when I want to go back to the image view again from didSelectRowAtIndexPath the app crashes and I get EXC_BREAKPOINT at
0x18e2756: calll 0x1a37a00 ; symbol stub for: getpid
I have tried debugging and it goes through viewWillAppear with seamingly no issues and then crashes
Any ideas on what the problem might be?
Thanks!
Edit
enabling zombies did not help, disabling arc changes nothing.

I fixed it by creating the new imageview when the row was selected instead of when the first view with the tableview was loaded, so my didselectrowatindexpath looks like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ImageViewController *view = [ImageViewController alloc];
view.imgPath = [[self.menus objectAtIndex:[indexPath row]] objectForKey:#"PATH"];
[self presentViewController:view animated:YES completion:nil];
}

Related

UIActivityIndicatorView showing and causing crash on two specific views

I am getting a very hard to trace crash in one of my views that is related to UIActivityIndicatorViewSometimes it crashes right away, sometimes it takes a few loads (going back to the previous view in the navigation stack and loading the view again). I have run the app through instruments, and while memory use keeps rising, I don't see any leaks. Could I be running out of memory? I also don't see any memory warnings. Here is the error:
*** -[UIActivityIndicatorView release]: message sent to deallocated instance 0x15f6d6aa0
I am not making any connections, and I can see the ActivityIndicator show up and then the app crashes when it disappears (or it disappears before the app crashes).
I have read that AFNetworking can cause this problem, but I am not using it. I am just using NSURLSession.
Both classes are very similar, and I have included the loading calls of one, as the class is too big to post here.
It is displayed in a Navigation Controller nested in a TabBarController:
- (BOOL)prefersStatusBarHidden {
return NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.userDefaults = [NSUserDefaults standardUserDefaults];
self.hairlineHeightConstraint.constant = 0.5f;
self.hairlineDivider.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
self.title = #"Course";
[self.classroomButton setTintColor:[UIColor pxColorWithHexValue:self.passedCourse.courseColor]];
[self.classroomButton addTarget:self action:#selector(subscribeToCourse:) forControlEvents:UIControlEventTouchUpInside];
[self getAnySubscribedCourse];
if (self.subscribedCourse) {
if (self.passedCourse == self.subscribedCourse) {
[self.classroomButton setTitle:#"Remove from Classroom" forState:UIControlStateNormal];
self.subscribedToThisCourse = YES;
} else {
self.alreadySubscribed = YES;
}
}
[self configureHeaderView];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (self.shareView) {
CGRect frame = self.shareView.frame;
CGFloat navbarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
frame = CGRectMake(0.0f, self.tableView.contentOffset.y + statusBarHeight + navbarHeight, frame.size.width, frame.size.height);
self.shareView.frame = frame;
[self.view bringSubviewToFront:self.shareView];
}
NSString *backText = self.navigationItem.backBarButtonItem.title;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:backText style:UIBarButtonItemStylePlain target:nil action:nil];
[backButton setTitleTextAttributes:[[MSThemeManager currentTheme] barButtonTextDictionaryForState:UIControlStateNormal withTintColor:[UIColor pxColorWithHexValue:self.passedCourse.courseColor]] forState:UIControlStateNormal];
self.navigationItem.backBarButtonItem = backButton;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
[self.tableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self.tableView.tableHeaderView isKindOfClass:[ParallaxCourseDetailHeaderView class]]) {
ParallaxCourseDetailHeaderView *headerView = (ParallaxCourseDetailHeaderView *)self.tableView.tableHeaderView;
[headerView.courseInfoAudioPlayer stop];
self.tableView.tableHeaderView = nil;
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - HeaderView
- (void)configureHeaderView
{
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
NSDictionary *attributesDictionary = #{NSFontAttributeName : self.headerLabel.font};
CGFloat width = screenWidth - self.headerLabel.frame.origin.x - 15.0f;
CGSize boundingBox = CGSizeMake(width, CGFLOAT_MAX);
NSString *descriptionToBound = self.passedCourse.courseDescription;
CGRect labelRect = [descriptionToBound boundingRectWithSize:boundingBox
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
CGRect headerLabelRect = self.headerLabel.frame;
CGRect tableHeaderViewRect = self.tableView.tableHeaderView.frame;
CGRect dividerRect = self.hairlineDivider.frame;
CGFloat heightDifference = ceilf(labelRect.size.height - headerLabelRect.size.height);
tableHeaderViewRect.size.height += heightDifference;
self.tableView.tableHeaderView.frame = tableHeaderViewRect;
dividerRect.origin.y += heightDifference;
headerLabelRect.size.width = ceil(labelRect.size.width);
headerLabelRect.size.height = ceil(labelRect.size.height);
headerLabelRect.origin.y = (dividerRect.origin.y - headerLabelRect.size.height) / 2;
self.headerLabel.frame = headerLabelRect;
CGFloat parallaxViewHeight = 280.0f;
CGFloat headerHeight = parallaxViewHeight + heightDifference;
ParallaxCourseDetailHeaderView *headerView = [[ParallaxCourseDetailHeaderView alloc] initWithTitleViewStyle:SWParallaxTitleViewStyleWhiteWithMask withHeight:tableHeaderViewRect.size.height headerHeight:headerHeight];
UIImage *image = [UIImage imageNaobj:self.passedCourse.headerImage];
headerView.courseImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[headerView setTintColor:[UIColor whiteColor]];
headerView.isTitleViewOpaque = YES;
headerView.fadeInDuration = 0.0;
headerView.trackID = self.passedCourse.aboutTrackID;
[headerView courseAudioIntroControl];
UIImage *headerTeacherImage = [UIImage imageNaobj:[NSString stringWithFormat:#"%#", self.passedCourse.teacher.teacherImageID]];
self.headerTeacherImage.image = headerTeacherImage;
self.headerTeacherImage.layer.cornerRadius = self.headerTeacherImage.frame.size.height / 2;
self.headerTeacherImage.clipsToBounds = YES;
self.headerLabel.text = self.passedCourse.courseDescription;
[self.headerTeacherButton setImage:headerTeacherImage forState:UIControlStateNormal];
UIView *oldHeaderView = self.tableView.tableHeaderView;
oldHeaderView.frame = CGRectMake(0, 0, screenWidth, oldHeaderView.frame.size.height);
self.tableView.tableHeaderView = nil;
[headerView.titleView addSubview:oldHeaderView];
self.tableView.tableHeaderView = headerView;
[headerView.imageContainerView setBackgroundColor:[UIColor pxColorWithHexValue:self.passedCourse.courseColor]];
headerView.title = self.passedCourse.title;
headerView.subtitle = [NSString stringWithFormat:#"%lu Sessions", (unsigned long)self.passedCourse.courseObjects.count];
headerView.teacher = [NSString stringWithFormat:#"With %#", self.passedCourse.teacher.teacherName];
[headerView.titleLabel adjustCurrentFontSize];
[headerView.subtitleLabel adjustCurrentFontSize];
[headerView.teacherLabel adjustCurrentFontSize];
}
#pragma mark - ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([self.tableView.tableHeaderView isKindOfClass:[ParallaxCourseDetailHeaderView class]]) {
ParallaxCourseDetailHeaderView *headerView = (ParallaxCourseDetailHeaderView *)self.tableView.tableHeaderView;
headerView.parallaxOffset = scrollView.contentOffset.y + scrollView.contentInset.top;
}
}

Image not Zoom On double tap in iOS

In my application having gallery functionality.For Scrolling image i used REPagedScrollView and i want to image zoom on pinch using 2 finger and also want zoom on double tap but i unable to to zoom image on double tap
please resolved my problem.
I tried by below code :
.h file
#interface ViewPhotoVC : UIViewController<UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
REPagedScrollView *scrollView;
UIScrollView *image_scroll;
int current_page;
BOOL is_FullScreen;
}
#property (strong,nonatomic) IBOutlet UIView *slider_view;
.m file
-(void)video_image_Gallery
{
scrollView = [[REPagedScrollView alloc] initWithFrame:self.slider_view.bounds];
scrollView.delegate=self;
// scrollView.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
// scrollView.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
current_page=0;
for(int i=0;i<[self.PhotoImgArr count];i++){
self.image_view = [[AsyncImageView alloc] initWithFrame:CGRectMake(0,0, self.slider_view.frame.size.width, self.slider_view.frame.size.height)];
// scrollView.frame=CGRectMake(0, 0, screenwidth, screenheight);
image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screenwidth,screenheight)];
self.image_view.backgroundColor=[UIColor clearColor];
self.image_view.contentMode = UIViewContentModeScaleAspectFit;
self.image_view.clipsToBounds = true;
self.image_view.userInteractionEnabled = YES;
self.image_view.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
self.image_view.image = [UIImage imageNamed:#"no_image1.png"];
self.image_view.imageURL =[NSURL URLWithString:[self.PhotoImgArr objectAtIndex:i]];
self.image_view.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:246.0/255.0 blue:241.0/255.0 alpha:1];
[image_scroll setDelegate:self];
[image_scroll setShowsHorizontalScrollIndicator:NO];
[image_scroll setShowsVerticalScrollIndicator:NO];
[image_scroll setMaximumZoomScale:8.0];
image_scroll.tag=i+1;
self.PlayVideoBtn.tag=i+1;
self.image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
image_scroll.contentSize = CGSizeMake(self.image_view.bounds.size.width-500, self.image_view.bounds.size.height-300);
image_scroll.decelerationRate = UIScrollViewDecelerationRateFast;
// [image_scroll setMinimumZoomScale:[image_scroll frame].size.width / [self.image_view frame].size.width];
[image_scroll setZoomScale:[image_scroll minimumZoomScale]];
[image_scroll addSubview:self.image_view];
[image_scroll addSubview:self.PlayVideoBtn];
[image_scroll addSubview:self.videoImg];
scrollView.tag=0;
// self.PlayVideoBtn.tag=0;
[scrollView addPage:image_scroll];
}
[self.slider_view addSubview:scrollView];
self.index_lbl.text = [NSString stringWithFormat:#"%d of %lu",(int)self.selected_index+1,(unsigned long)[self.PhotoImgArr count]];
[scrollView scrollToPageWithIndex:self.selected_index animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
if(scrollView1.tag ==0){
CGFloat pageWidth = scrollView1.frame.size.width;
current_page = floor((scrollView1.contentOffset.x - pageWidth / 2.0) / pageWidth) + 1;
self.index_lbl.text = [NSString stringWithFormat:#"%d of %lu",current_page+1,(unsigned long)[self.PhotoImgArr count]];
if(current_page+1==self.PhotoImgArr.count)
{
self.nextBtn.userInteractionEnabled=NO;
self.nextImg.image=[UIImage imageNamed:#"disable_next.png"];
}
else if(current_page==0)
{
self.prevBtn.userInteractionEnabled=NO;
self.prevImg.image=[UIImage imageNamed:#"disable_prev.png"];
}
else
{
self.nextBtn.userInteractionEnabled=YES;
self.prevBtn.userInteractionEnabled=YES;
self.nextImg.image=[UIImage imageNamed:#"next.png"];
self.prevImg.image=[UIImage imageNamed:#"prev.png"];
}
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView1 {
if([self.PhotoOrVideo isEqualToString:#"Photo"])
{
if([[scrollView1 viewWithTag:scrollView1.tag].subviews count]>0){
return [[scrollView1 viewWithTag:scrollView1.tag].subviews objectAtIndex:0];
}
}
return nil;
}
Please try the below code:
-(void)setScrollView:(UIScrollView *)scrollView
{
_scrollView=scrollView;
_scrollView.minimumZoomScale=0.2;
_scrollView.maximumZoomScale=3.0;
_scrollView.delegate=self;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
[self.scrollView flashScrollIndicators];
}

UITableView Layout is Broken

I'm new to iOS development, and now working on an iOS app mainly using UINavigationController and UITableViewController.
The problem I try to solve is the UITableView (Custom tableView cell) layout broken bug on the root viewController. This happens when I back from a child viewController to the root viewController.(Left: Original, Right: Wrong Layout) It looks like UITableView position is moved for some reason, but I'm not sure why.
In MainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
viewFrame = self.view.bounds;
[self loadHeader];
[self loadTable];
self.view.backgroundColor = [UIColor whiteColor];
numberOfCellRow = 20;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)loadView {
[super loadView];
}
- (void)loadHeader {
CGRect headerFrame = viewFrame;
headerFrame.size.width = self.view.frame.size.width;
headerFrame.size.height = 200;
self.headerView = [[HeaderView alloc] initWithFrame:CGRectIntegral(headerFrame)];
[self.view addSubview:self.headerView];
NSLog(#"header loaded");
}
- (void)loadTable {
CGRect tableFrame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y + 150,
viewFrame.size.width, viewFrame.size.height-150);
self.customTableView = [[UITableView alloc] initWithFrame:CGRectIntegral(tableFrame)];
self.customTableView.backgroundColor = [UIColor clearColor];
//set delegates
self.customTableView.delegate = self;
self.customTableView.dataSource = self;
//set custom cell
UINib *nib = [UINib nibWithNibName:#"CardViewCell" bundle:nil];
[self.customTableView registerNib:nib forCellReuseIdentifier:#"Cell"];
self.customTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.customTableView];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell loaded");
dispatch_async(dispatch_get_main_queue(), ^{
[self loadData:indexPath];
});
}
- (void)loadData: (NSIndexPath *)indexPath {
_tableCell.titleLabel.text = [[_articles objectAtIndex:indexPath.row] title];
NSLog(#"%#", [[_articles objectAtIndex:indexPath.row] title]);
[_tableCell setNeedsLayout];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
_tableCell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
_tableCell.backgroundColor = [UIColor clearColor];
[_tableCell.contentView setUserInteractionEnabled: NO];
// add touch events
[_tableCell.button addTarget:self
action:#selector(cellPressed:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
NSLog(#"%#", NSStringFromCGRect(tableView.frame));
return _tableCell;
}
In CardViewCell.m
- (void)drawRect:(CGRect)rect {
//self.backgroundColor = [UIColor colorWithRed:228/255.0f green:228/255.0f blue:228/255.0f alpha:1.0f];
NSLog(#"%f", rect.origin.x);
self.backgroundColor = [UIColor clearColor];
[self drawCardView];
[self drawCardImage];
[self drawButton];
[self drawLabels];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)drawCardImage {
_cardViewBounds = _cardView.bounds;
CGRect imageFrame = _cardViewBounds;
imageFrame.size.height = _cardViewBounds.size.height/2+40;
_cardImage = [[UIImageView alloc] initWithFrame:CGRectIntegral(imageFrame)];
_cardImage.image = [UIImage imageNamed:#"sampleImage"];
_cardImage.backgroundColor = [UIColor grayColor];
_cardImage.clipsToBounds = YES;
_cardImage.contentMode = UIViewContentModeScaleAspectFill;
_cardImage.userInteractionEnabled = YES;
_cardImage.tag = 100;
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cardView.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(3.0, 3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.cardView.bounds;
maskLayer.path = maskPath.CGPath;
_cardImage.layer.mask = maskLayer;
[self.cardView addSubview:_cardImage];
}
- (void)drawCardView {
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect frame = CGRectMake(15, 25, (int)screen.size.width-30, 200);
_cardView.layer.cornerRadius = 3;
_cardView.backgroundColor = [UIColor greenColor];
_cardImage.userInteractionEnabled = YES;
_cardImage.tag = 101;
_cardView.frame = CGRectIntegral(frame);
CALayer *caLayer = _cardView.layer;
caLayer.frame = _cardView.frame;
caLayer.shadowRadius = 3.0f;
caLayer.shadowOpacity = 0.4f;
caLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
caLayer.shouldRasterize = YES;
// retina screen resolution
[caLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
[caLayer setShouldRasterize:YES];
[self addSubview:_cardView];
}
- (void)drawButton {
CGRect buttonSize = CGRectMake(_cardViewBounds.size.width-100, _cardViewBounds.size.height/2+10, 50, 50);
_button = [[UIButton alloc] initWithFrame:CGRectIntegral(buttonSize)];
_button.layer.cornerRadius = 25;
_button.backgroundColor = [UIColor colorWithRed:0 green:169.0f/255.0f blue:244.0f/255.0f alpha:1.0f];
_button.userInteractionEnabled = YES;
_button.tag = 102;
//_button.titleLabel.text = #"+";
[_button setTitle:#"+" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:30.0]];
CALayer *caButtonLayer = _button.layer;
caButtonLayer.frame = _button.frame;
caButtonLayer.shadowRadius = 3.0f;
caButtonLayer.shadowOpacity = 0.2f;
caButtonLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
caButtonLayer.shouldRasterize = YES;
// retina screen resolution
[caButtonLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
[caButtonLayer setShouldRasterize:YES];
[self.cardView addSubview:_button];
}
- (void)drawLabels {
CGRect titleLabelSize = CGRectMake( 10, _cardViewBounds.size.height-50, _cardViewBounds.size.width-20, 40);
_titleLabel = [[UILabel alloc] initWithFrame:CGRectIntegral(titleLabelSize)];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.numberOfLines = 2;
_titleLabel.userInteractionEnabled = YES;
_titleLabel.tag = 103;
[_titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:15.0]];
[_cardView addSubview:_titleLabel];
[_cardView sendSubviewToBack:_titleLabel];
}
- (void)prepareForReuse {
[super prepareForReuse];
for (UIView *subView in [self.contentView subviews]) {
[subView removeFromSuperview];
}
}
The code of this app is on this link.
Any ideas to solve this?
Your thoughts and help will be hugely appreciated.
the reason the view misplace is due to the _cardView.frame is CGRectZero when disappear, and I didn't find the solve yet.
you can add those to see the change.
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
NSLog(#"viewWillLayoutSubviews cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
NSLog(#"viewDidLayoutSubviews cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(#"viewWillDisappear cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(#"viewDidDisappear cardView %#",NSStringFromCGRect(self.tableCell.cardView.frame));
}
I think you can learn more about life-cycle about viewController and Cell.
LifeCycle
by the way, when I watching your code, I don't know if you want to use xib or only code to programming. Like the code below, this is init from code but you try to get from xib.
just my opinion.
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// make self from nib file
UINib *nib = [UINib nibWithNibName:#"HeaderView" bundle:nil];
self = [nib instantiateWithOwner:nil options:nil][0];
self.backgroundColor = [UIColor colorWithRed:0 green:169.0f/255.0f blue:244.0f/255.0f alpha:1.0f];
CGRect frame = CGRectZero;
frame.size.width = [[UIScreen mainScreen] bounds].size.width;
frame.size.height = 200;
[self setFrame:frame];
}
return self;
}

Symmetric zooming of image within scrollview in iOS

I am new to iPhone to iOS development. and I want a demo for zoom in and zoom out (symmetric zooming of image) a image within a scrollview in iOS.This is my code : in this code i have taken an image view within a scrollview(ZoomScrollView) and scrollview within a another Scrollview(scrollView_Gallery).Thanks in advance..
//ZoomGalleryViewController.m//
scrollView_Gallery.backgroundColor = [UIColor yellowColor];
scrollView_Gallery.delegate = self;
scrollView_Gallery.pagingEnabled = YES;
scrollView_Gallery.userInteractionEnabled = YES;
scrollView_Gallery.showsHorizontalScrollIndicator = NO;
scrollView_Gallery.showsVerticalScrollIndicator = NO;
[self.view addSubview:scrollView_Gallery];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
[scrollView_Gallery setContentSize:CGSizeMake(self.view.frame.size.width*[arrayUrl count],self.view.frame.size.height)];
}
else if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
[scrollView_Gallery setContentSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width*[arrayUrl count],[[UIScreen mainScreen] bounds].size.height)];
}
for (int i = 0; i < [arrayUrl count]; i++)
{
zoomScrollView = [[ZoomScrollView alloc]init];
zoomScrollView.backgroundColor = [UIColor redColor];
[zoomScrollView setContentSize:CGSizeMake(zoomScrollView.contentSize.width,zoomScrollView.frame.size.height)];
CGRect frame = scrollView_Gallery.frame;
frame.origin.x = (frame.size.width)* i;
frame.origin.y = 0;
zoomScrollView.frame = frame;
zoomScrollView.imageView.contentMode = UIViewContentModeScaleAspectFit;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[[arrayUrl objectAtIndex:i]objectForKey:#"photopath"]]];
__weak UIImageView *weakImageView = zoomScrollView.imageView;
zoomScrollView.imageView.tag = i;
zoomScrollView.ZOOM_VIEW_TAG = zoomScrollView.imageView.tag;
[zoomScrollView.imageView setImageWithURLRequest:request
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
NSLog(#"image=%f,imageheight=%f",image.size.width,image.size.height);
UIImageView *strongImageView = weakImageView; // make local strong reference to protect against race conditions
strongImageView.frame = CGRectMake(0,0,image.size.width*image.scale, image.size.height*image.scale);
strongImageView.center = scrollView_Gallery.center;
NSLog(#"image11 = %f, imageheight11 = %f",image.size.width*image.scale, image.size.height*image.scale);
NSLog(#"imageScale = %f",image.scale);
if (!strongImageView) return;
[UIView transitionWithView:strongImageView
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
strongImageView.image = image;
}
completion:NULL];
//[scrollView_Gallery setContentOffset:CGPointMake(strongImageView.frame.size.width * (indexNumber+1), 0)];
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(#"Request failed with error: %#", error);
}];
[scrollView_Gallery addSubview:zoomScrollView];
[zoomScrollView setContentSize:CGSizeMake(zoomScrollView.contentSize.width,zoomScrollView.frame.size.height)];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
[scrollView_Gallery setContentOffset:CGPointMake(self.view.frame.size.width * (indexNumber), 0)];
}
else if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
[scrollView_Gallery setContentOffset:CGPointMake([[UIScreen mainScreen] bounds].size.width * (indexNumber), 0)];
}
// ZoomScrollView.h//
#import <UIKit/UIKit.h>
#interface MRZoomScrollView : UIScrollView <UIScrollViewDelegate>
{
UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
#end
// MRZoomScrollView.m//
#import "MRZoomScrollView.h"
#define MRScreenWidth CGRectGetWidth([UIScreen mainScreen].applicationFrame)
#define MRScreenHeight CGRectGetHeight([UIScreen mainScreen].applicationFrame)
#interface MRZoomScrollView (Utility)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
#end
#implementation MRZoomScrollView
#synthesize imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
self.frame = CGRectMake(0, 0, MRScreenWidth, MRScreenHeight);
[self initImageView];
}
return self;
}
- (void)initImageView
{
imageView = [[UIImageView alloc]init];
// The imageView can be zoomed largest size
imageView.frame = CGRectMake(0, 0, MRScreenWidth * 2.5, MRScreenHeight * 2.5);
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
[imageView release];
// Add gesture,double tap zoom imageView.
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTapGesture setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];
float minimumScale = self.frame.size.width / imageView.frame.size.width;
[self setMinimumZoomScale:minimumScale];
[self setZoomScale:minimumScale];
}
#pragma mark - Zoom methods
- (void)handleDoubleTap:(UIGestureRecognizer *)gesture
{
float newScale = self.zoomScale * 1.5;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
[self zoomToRect:zoomRect animated:YES];
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
CGRect zoomRect;
zoomRect.size.height = self.frame.size.height / scale;
zoomRect.size.width = self.frame.size.width / scale;
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
[scrollView setZoomScale:scale animated:NO];
}
You can do it by using UIScrollView and enable enabledZoom ON.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
in viewDidLoad method paste this code
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.delegate=self;
}
Look at this tutorial, it was helpful for me:
Assuming you have a property scrollView, try the following code in your view controller s viewDidLoad or viewWillAppear methods:
self.scrollView.minimumZoomScale=1.0;
self.scrollView.maximumZoomScale=2.0;
self.scrollView.contentSize=self.scrollView.frame.size;
self.scrollView.bounces=YES;
self.scrollView.delegate=self;
Let your view controller conform to the UIScrollViewDelegate protocol. And implement the delegate method :
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;// return the imageView inside the scrollview which has to be scaled.
}

iOS Implicit conversion of 'int' to 'UIColor*' is disallowed with ARC

I have a pickerview that is getting its information from a separate AFPickerView file. I'm getting the error "Implicit conversion of 'int' to 'UIColor*' is disallowed with ARC". Any suggestions on an easy fix for this?
Here is the code that it effects:
[visibleViews minusSet:recycledViews];
// add missing pages
for (int index = firstNeededViewIndex; index <= lastNeededViewIndex; index++)
{
if (![self isDisplayingViewForIndex:index])
{
UILabel *label = (UILabel *)[self dequeueRecycledView];
if (label == nil)
{
label = [[UILabel alloc] initWithFrame:CGRectMake(_rowIndent, 0, self.frame.size.width - _rowIndent, 39.0)];
label.backgroundColor = [UIColor clearColor];
label.font = self.rowFont;
label.textColor = RGBACOLOR(0.0, 0.0, 0.0, 0.75);
}
[self configureView:label atIndex:index];
[contentView addSubview:label];
[visibleViews addObject:label];
}
}
}
Here is the full code:
#import "AFPickerView.h"
#implementation AFPickerView
#pragma mark - Synthesization
#synthesize dataSource;
#synthesize delegate;
#synthesize selectedRow = currentRow;
#synthesize rowFont = _rowFont;
#synthesize rowIndent = _rowIndent;
#pragma mark - Custom getters/setters
- (void)setSelectedRow:(int)selectedRow
{
if (selectedRow >= rowsCount)
return;
currentRow = selectedRow;
[contentView setContentOffset:CGPointMake(0.0, 39.0 * currentRow) animated:NO];
}
- (void)setRowFont:(UIFont *)rowFont
{
_rowFont = rowFont;
for (UILabel *aLabel in visibleViews)
{
aLabel.font = _rowFont;
}
for (UILabel *aLabel in recycledViews)
{
aLabel.font = _rowFont;
}
}
- (void)setRowIndent:(CGFloat)rowIndent
{
_rowIndent = rowIndent;
for (UILabel *aLabel in visibleViews)
{
CGRect frame = aLabel.frame;
frame.origin.x = _rowIndent;
frame.size.width = self.frame.size.width - _rowIndent;
aLabel.frame = frame;
}
for (UILabel *aLabel in recycledViews)
{
CGRect frame = aLabel.frame;
frame.origin.x = _rowIndent;
frame.size.width = self.frame.size.width - _rowIndent;
aLabel.frame = frame;
}
}
#pragma mark - Initialization
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// setup
[self setup];
// backgound
UIImageView *bacground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pickerBackground.png"]];
[self addSubview:bacground];
// content
contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)];
contentView.showsHorizontalScrollIndicator = NO;
contentView.showsVerticalScrollIndicator = NO;
contentView.delegate = self;
[self addSubview:contentView];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTap:)];
[contentView addGestureRecognizer:tapRecognizer];
// shadows
UIImageView *shadows = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pickerShadows.png"]];
[self addSubview:shadows];
// glass
UIImage *glassImage = [UIImage imageNamed:#"pickerGlass.png"];
glassImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 76.0, glassImage.size.width, glassImage.size.height)];
glassImageView.image = glassImage;
[self addSubview:glassImageView];
}
return self;
}
- (void)setup
{
_rowFont = [UIFont boldSystemFontOfSize:24.0];
_rowIndent = 30.0;
currentRow = 0;
rowsCount = 0;
visibleViews = [[NSMutableSet alloc] init];
recycledViews = [[NSMutableSet alloc] init];
}
#pragma mark - Buisness
- (void)reloadData
{
// empry views
currentRow = 0;
rowsCount = 0;
for (UIView *aView in visibleViews)
[aView removeFromSuperview];
for (UIView *aView in recycledViews)
[aView removeFromSuperview];
visibleViews = [[NSMutableSet alloc] init];
recycledViews = [[NSMutableSet alloc] init];
rowsCount = [dataSource numberOfRowsInPickerView:self];
[contentView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
contentView.contentSize = CGSizeMake(contentView.frame.size.width, 39.0 * rowsCount + 4 * 39.0);
[self tileViews];
}
- (void)determineCurrentRow
{
CGFloat delta = contentView.contentOffset.y;
int position = round(delta / 39.0);
currentRow = position;
[contentView setContentOffset:CGPointMake(0.0, 39.0 * position) animated:YES];
[delegate pickerView:self didSelectRow:currentRow];
}
- (void)didTap:(id)sender
{
UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)sender;
CGPoint point = [tapRecognizer locationInView:self];
int steps = floor(point.y / 39) - 2;
[self makeSteps:steps];
}
- (void)makeSteps:(int)steps
{
if (steps == 0 || steps > 2 || steps < -2)
return;
[contentView setContentOffset:CGPointMake(0.0, 39.0 * currentRow) animated:NO];
int newRow = currentRow + steps;
if (newRow < 0 || newRow >= rowsCount)
{
if (steps == -2)
[self makeSteps:-1];
else if (steps == 2)
[self makeSteps:1];
return;
}
currentRow = currentRow + steps;
[contentView setContentOffset:CGPointMake(0.0, 39.0 * currentRow) animated:YES];
[delegate pickerView:self didSelectRow:currentRow];
}
#pragma mark - recycle queue
- (UIView *)dequeueRecycledView
{
UIView *aView = [recycledViews anyObject];
if (aView)
[recycledViews removeObject:aView];
return aView;
}
- (BOOL)isDisplayingViewForIndex:(NSUInteger)index
{
BOOL foundPage = NO;
for (UIView *aView in visibleViews)
{
int viewIndex = aView.frame.origin.y / 39.0 - 2;
if (viewIndex == index)
{
foundPage = YES;
break;
}
}
return foundPage;
}
- (void)tileViews
{
// Calculate which pages are visible
CGRect visibleBounds = contentView.bounds;
int firstNeededViewIndex = floorf(CGRectGetMinY(visibleBounds) / 39.0) - 2;
int lastNeededViewIndex = floorf((CGRectGetMaxY(visibleBounds) / 39.0)) - 2;
firstNeededViewIndex = MAX(firstNeededViewIndex, 0);
lastNeededViewIndex = MIN(lastNeededViewIndex, rowsCount - 1);
// Recycle no-longer-visible pages
for (UIView *aView in visibleViews)
{
int viewIndex = aView.frame.origin.y / 39 - 2;
if (viewIndex < firstNeededViewIndex || viewIndex > lastNeededViewIndex)
{
[recycledViews addObject:aView];
[aView removeFromSuperview];
}
}
[visibleViews minusSet:recycledViews];
// add missing pages
for (int index = firstNeededViewIndex; index <= lastNeededViewIndex; index++)
{
if (![self isDisplayingViewForIndex:index])
{
UILabel *label = (UILabel *)[self dequeueRecycledView];
if (label == nil)
{
label = [[UILabel alloc] initWithFrame:CGRectMake(_rowIndent, 0, self.frame.size.width - _rowIndent, 39.0)];
label.backgroundColor = [UIColor clearColor];
label.font = self.rowFont;
label.textColor = RGBACOLOR(0.0, 0.0, 0.0, 0.75);
}
[self configureView:label atIndex:index];
[contentView addSubview:label];
[visibleViews addObject:label];
}
}
}
- (void)configureView:(UIView *)view atIndex:(NSUInteger)index
{
UILabel *label = (UILabel *)view;
label.text = [dataSource pickerView:self titleForRow:index];
CGRect frame = label.frame;
frame.origin.y = 39.0 * index + 78.0;
label.frame = frame;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self tileViews];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
[self determineCurrentRow];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self determineCurrentRow];
}
#end
This should work with ARC. Replace RGBACOLOR with colorWithRed:green:blue:alpha
label.textColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.75f];

Resources