Below is the code that I'm using in my app for a custom segmented control with on/off colors. However, with iOS 6, when the view first loads it is the default colors. Once you choose one of the segments, then the proper custom colors appear. What do I need to do to have it appear when the view loads and ensure it's compatible for devices not on iOS 6.
#define kTagFirst 111
#define kTagSecond 112
#define kTagThird 113
- (IBAction)segmentedControlChanged:(id)sender
{
UIColor *darkBG = [UIColor colorWithRed:149.0/255.0 green:133.0/255.0 blue:115.0/255.0 alpha:1.0];
UIColor *lightBG = [UIColor colorWithRed:234.0/255.0 green:232.0/255.0 blue:217.0/255.0 alpha:1.0];
UIColor *darkText = [UIColor colorWithRed:85.0/255.0 green:72.0/255.0 blue:60.0/255.0 alpha:1.0];
UIColor *lightText = [UIColor whiteColor];
[segmentedControl setTintColor:darkBG forTag:kTagFirst];
[segmentedControl setTintColor:darkBG forTag:kTagSecond];
[segmentedControl setTintColor:darkBG forTag:kTagThird];
// [segmentedControl setTextColor:lightText forTag:kTagFirst];
// [segmentedControl setTextColor:lightText forTag:kTagSecond];
// [segmentedControl setTextColor:lightText forTag:kTagThird];
switch (segmentedControl.selectedSegmentIndex)
{
case 0:
mapView.hidden = YES;
infoView.hidden = YES;
[segmentedControl setTintColor:darkBG forTag:kTagFirst];
[segmentedControl setTintColor:lightBG forTag:kTagSecond];
[segmentedControl setTintColor:lightBG forTag:kTagThird];
[segmentedControl setTextColor:lightText forTag:kTagFirst];
[segmentedControl setTextColor:darkText forTag:kTagSecond];
[segmentedControl setTextColor:darkText forTag:kTagThird];
//[segmentedControl setShadowColor:lightBG forTag:kTagFirst];
[segmentedControl setShadowColor:lightBG forTag:kTagSecond];
[segmentedControl setShadowColor:lightBG forTag:kTagThird];
break;
case 1:
mapView.hidden = NO;
infoView.hidden = YES;
[segmentedControl setTintColor:lightBG forTag:kTagFirst];
[segmentedControl setTintColor:darkBG forTag:kTagSecond];
[segmentedControl setTintColor:lightBG forTag:kTagThird];
[segmentedControl setTextColor:darkText forTag:kTagFirst];
[segmentedControl setTextColor:lightText forTag:kTagSecond];
[segmentedControl setTextColor:darkText forTag:kTagThird];
[segmentedControl setShadowColor:lightBG forTag:kTagFirst];
//[segmentedControl setShadowColor:lightBG forTag:kTagSecond];
[segmentedControl setShadowColor:lightBG forTag:kTagThird];
break;
case 2:
mapView.hidden = YES;
infoView.hidden = NO;
[segmentedControl setTintColor:lightBG forTag:kTagFirst];
[segmentedControl setTintColor:lightBG forTag:kTagSecond];
[segmentedControl setTintColor:darkBG forTag:kTagThird];
[segmentedControl setTextColor:darkText forTag:kTagFirst];
[segmentedControl setTextColor:darkText forTag:kTagSecond];
[segmentedControl setTextColor:lightText forTag:kTagThird];
[segmentedControl setShadowColor:lightBG forTag:kTagFirst];
[segmentedControl setShadowColor:lightBG forTag:kTagSecond];
//[segmentedControl setShadowColor:lightBG forTag:kTagThird];
break;
}
}
- (void)viewDidLoad
{
[segmentedControl setTag:kTagFirst forSegmentAtIndex:2];
[segmentedControl setTag:kTagSecond forSegmentAtIndex:1];
[segmentedControl setTag:kTagThird forSegmentAtIndex:0];
[self segmentedControlChanged:nil];
mapView.hidden = YES;
infoView.hidden = YES;
segmentedControl.selectedSegmentIndex = 0;
you need to move some logic into view did load method that sets the default tint color for your segment bar on load. right now, you have all that logic in your IBAction - which probably does not get called until it is first selected.
hope that helps
Related
I have an issue with a UITableViewController where as soon as you scroll down the background image of the cell increases in size. All the images are set to a width of 320px but still after you scroll the cell background image expands.
Can anyone explain why this is happening and how to fix it?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (jsonResults.count == 0)
{
return 1;
}
else
{
return [jsonResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIImage *selectionBackground;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (jsonResults.count == 0)
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"topAndBottomRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
[cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];
cell.textLabel.text = #"Nothing was found :(";
cell.detailTextLabel.text = #"Click here to add a venue!";
//cell.textLabel.textColor= [UIColor colorWithRed:(79/255.0) green:(129/255.0) blue:(189/255.0) alpha:1] ;
cell.textLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:22];
cell.detailTextLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:14];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.userInteractionEnabled = YES;
NSDictionary *venuesdict = [jsonResults objectAtIndex:indexPath.row];
NSString *addresslineone = [venuesdict objectForKey:#"address1"];
NSString *postcode = [venuesdict objectForKey:#"postcode"];
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
//rowBackground = [UIImage imageNamed:#"topAndBottomRow.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"topAndBottomRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
//rowBackground = [UIImage imageNamed:#"topRow.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"topRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
selectionBackground = [UIImage imageNamed:#"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
//rowBackground = [UIImage imageNamed:#"bottomRow.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"bottomRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
//rowBackground = [UIImage imageNamed:#"bottomRow.png"];
selectionBackground = [UIImage imageNamed:#"bottomRowSelected.png"];
}
else
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"middleRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
//rowBackground = [UIImage imageNamed:#"middleRow.png"];
selectionBackground = [UIImage imageNamed:#"middleRowSelected.png"];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[imgView setImage:selectionBackground];
[cell.selectedBackgroundView addSubview:imgView];
[cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];
cell.textLabel.text = [venuesdict objectForKey:#"name"];
NSString *imagename = [venuesdict objectForKey:#"imagename"];
cell.imageView.image = [UIImage imageNamed:imagename];
cell.detailTextLabel.text = [NSString stringWithFormat:#" %#, %#",addresslineone,postcode];
cell.textLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:22];
cell.detailTextLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:10];
cell.backgroundColor = [UIColor clearColor];
NSString *version = [[UIDevice currentDevice] systemVersion];
BOOL isAtLeast7 = [version floatValue] >= 7.0;
if (!isAtLeast7 == YES)
{
cell.detailTextLabel.backgroundColor = [UIColor grayColor];
cell.textLabel.backgroundColor = [UIColor grayColor];
}
}
return cell;
}
Try to change this line
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];
into this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.bounds];
I'm doing an application that requires core plot for drawing charts, I'm new to this library and I am finding it pretty hard to find good documentation or examples. I'm running into a problem where the line for the graph is not being display despite the fact that the data source method is getting called and returning the right number at the right index. Also the x Axis is being displayed wrong (Check the image below(1.0)). The Y axis is set correctly and the increment is also correct. I've been playing around trying to figure out what it's wrong but I spent too much time already so I was hoping to find some one here that could help or point me at the right direction. This is my implementation file :
-(void)initPlot {
[self generateData];
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
- (void)generateData{
//Array containing all the dates that will be displayed on the X axis
dates = [NSArray arrayWithObjects:#"Apr 25", #"Apr 26", #"Apr 29",#"Apr 30", #"May 1", nil];
//Dictionary containing the name of the single set and its associated color
sets = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], #"Plot 1",nil];
_dataY = [[NSMutableArray alloc] init];
[_dataY insertObject:[NSNumber numberWithFloat:618.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:613.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:613.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:614.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:604.0] atIndex:0];
_dataForPlot = [[NSMutableArray alloc] init];
for(int i = 0; i < dates.count; i++){
NSString *date = [dates objectAtIndex:i];
NSNumber *price = [_dataY objectAtIndex:i];
NSMutableDictionary *point1 = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:date, #"x", price, #"y", nil] autorelease];
[_dataForPlot addObject:point1];
}
NSLog(#"Data %#",_dataForPlot);
}
-(void)configureHost {
_hostView.allowPinchScaling = NO;
}
-(void)configureGraph {
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainBlackTheme]];
_hostView.hostedGraph = graph;
graph.plotAreaFrame.masksToBorder = NO;
// Configure the Graph Padding
graph.paddingLeft = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingBottom = 0.0f;
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor whiteColor];
borderLineStyle.lineWidth = 2.0f;
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
graph.plotAreaFrame.paddingTop = 10.0;
graph.plotAreaFrame.paddingRight = 10.0;
graph.plotAreaFrame.paddingBottom = 40.0;
graph.plotAreaFrame.paddingLeft = 70.0;
// Set graph title
graph.title = #"Test";
// Create and set text style
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = #"Helvetica-Bold";
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
graph.plotAreaFrame.borderLineStyle = nil;
}
- (void)configurePlots{
CPTColor *aColor = [CPTColor redColor];
CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0;
barLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
// Enable user interactions for plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(5.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat([self lowerValue]) length:CPTDecimalFromFloat([self higherValue])];
dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"Plot 1";
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor :[CPTColor greenColor]
endingColor :[CPTColor blackColor]];
areaGradient.angle = -90.0f ;
CPTFill *areaGradientFill = [ CPTFill fillWithGradient :areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPTDecimalFromString (#"0.0");
dataSourceLinePlot.interpolation = CPTScatterPlotInterpolationLinear;
// Set up plot space
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.4f)];
plotSpace.yRange = yRange;
// Create styles and symbols
CPTMutableLineStyle *aLineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
aLineStyle.lineWidth = 1.0;
aLineStyle.lineColor = aColor;
dataSourceLinePlot.dataLineStyle = aLineStyle;
//Add legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
theLegend.numberOfRows = sets.count;
theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(15.0, 15.0);
whiteTextStyle.fontSize = 13.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 5.0;
theLegend.paddingLeft = 10.0;
theLegend.paddingTop = 10.0;
theLegend.paddingRight = 10.0;
theLegend.paddingBottom = 10.0;
graph.legend = theLegend;
graph.legendAnchor = CPTRectAnchorTopLeft;
graph.legendDisplacement = CGPointMake(80.0, -10.0);
}
- (void)configureAxes{
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = #"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
// Line Style
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
//Axises
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
//Y axis
CPTXYAxis *y = axisSet.yAxis;
y.title = #"Price";
y.titleOffset = 50.0f;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y. majorIntervalLength = CPTDecimalFromString(#"5");
y.minorTicksPerInterval = 4;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
y.minorTickLineStyle = nil;
y.labelOffset = 2.0f;
// Configure x-axis
CPTXYAxis *x = axisSet.xAxis;
x. majorIntervalLength = CPTDecimalFromString (#"5");
x.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
x.majorIntervalLength = CPTDecimalFromInt(5);
x.minorTicksPerInterval = 0;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorGridLineStyle = majorGridLineStyle;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[_dataForPlot count]];
static CPTMutableTextStyle *labelTextStyle = nil;
labelTextStyle = [[CPTMutableTextStyle alloc] init];
labelTextStyle.color = [CPTColor whiteColor];
labelTextStyle.fontSize = 10.0f;
int index = 0;
for(NSString *date in dates){
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:date textStyle:labelTextStyle];
newLabel.tickLocation = CPTDecimalFromInt(index);
newLabel.offset = x.labelOffset + x.majorTickLength + 5;
newLabel.rotation = M_PI / 4;
[customLabels addObject:newLabel];
[newLabel release];
index++;
}
x.axisLabels = [NSSet setWithArray:customLabels];
}
- (float)higherValue{
NSNumber* max = [_dataY valueForKeyPath:#"#max.self"];
return [max floatValue];
}
- (float)lowerValue{
NSNumber* min = [_dataY valueForKeyPath:#"#min.self"];
return [min floatValue];
}
The Data Source Methods :
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return dates.count;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSString *key = (fieldEnum == CPTScatterPlotFieldX ? #"x" : #"y");
NSNumber *num = 0;
//if ( [(NSString *)plot.identifier isEqualToString:#"Plot 1"] ) {
num = [[_dataForPlot objectAtIndex:index] valueForKey:key];
if ( fieldEnum == CPTScatterPlotFieldX ) {
num = 0;
}
//}
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:2.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:#"animateOpacity"];
NSLog(#"NUM : %# for key : %# at index : %i",num,key,index);
return num;
}
This is the image :
The x-axis is displayed exactly as you told it. It ranges between -0.25 and 5.25 (0 to 5 expanded by 10%) with five labels at 0, 1, 2, 3, and 4.
The datasource returns nil (0) for the CPTScatterPlotFieldX field for every point. This tells the plot to ignore that point. Based on the plot place range you've set up, you should return an NSNumber containing the index for that field.
I have a project with a dozen different view controllers. All use the same code to set the background of their navigation bars as follows:
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:28]].width, 44);
UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
titleLabel.text = #"Categories";
titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
titleLabel.backgroundColor = UIColorFromRGBWithAlpha(0x84A537, 0.5);
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
self.navigationController.navigationBar.tintColor = UIColorFromRGBWithAlpha(0x84A537, 0.5);
self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
self.navigationController.view.backgroundColor = UIColorFromRGBWithAlpha(0x84A537, 0.1);
They all work fine, except for the CollectionView where the title text has a grey background. I've copied and pasted the code from other VCs. I don't know what I'm doing to cause this. I set the color hex values like this:
//RGB color macro
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//RGB color macro with alpha
#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]
It seems that the navigation bar style settings for Collection views are different from other view controllers. For these, I had to set the bar color in the calling controller, and set the nav. bar background color in the collection view to clearColor.
I hope this helps others.
calling VC:
- (IBAction)imageButtonTapped:(id)sender {
imageCellLayout = [[ImageCellLayout alloc] init];
imageViewController= [[ImageViewController alloc] initWithCollectionViewLayout:imageCellLayout];
imageViewController.item = item;
addImageViewController.item = item;
imageViewController.collectionView.pagingEnabled = YES;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:imageViewController];
nc.navigationBar.tintColor = UIColorFromRGBWithAlpha(0xC43F32, 0.5);
nc.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:nc animated:YES completion:nil];
}
collection view:
//Title Stuff
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:28]].width, 44);
UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
NSString *tempTitle = item.title;
titleLabel.text = tempTitle;
titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
I have a UITable, and I like to add an image in the detail view for a cell. I can handle the selection for any image from the camera roll or from the camera:
cell.imageView.image = someImage;
But how do I define a specific image - in the case above: "someImage", so that the next time the app is run, the correct image is shown for each item.
UPDATE. This is the code I'm using to snap/select an image..
- (IBAction)btnTakePicture_Clicked:(id)sender
{
NSLog(#"%s", __FUNCTION__);
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..." delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Image Gallary", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%s", __FUNCTION__);
switch (actionSheet.tag)
{
case 1:
switch (buttonIndex)
{
case 0:
{
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
//picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
#endif
}
break;
case 1:
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
break;
}
break;
default:
break;
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSLog(#"%s", __FUNCTION__);
dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
imgPicture.image = [[UIImage alloc] initWithData:dataImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
UPDATE 2. With the help from people below, I think this solution will work for me:
- (IBAction)photoLibraryAction:(id)sender
{
int c = self.capturedImages.count;
for (int i=0; i < c; i++ ){
if (self.imageView.tag == cellTag) {
NSLog(#"found it");
} else {
NSLog(#"can't find it");
}
}
}
if ([self.capturedImages count] == 1)
{
// we took a single shot
[self.imageView setImage:[self.capturedImages objectAtIndex:0]];
[self.imageView setTag:myTag];
}
I would do in this way.
#interface CustomClassCell : UITableViewCell
#property (unsafe_unretained, nonatomic) IBOutlet UIImageView *HomePicture;
#end
#interface CustomClass : NSObject
#property (nonatomic, copy) NSString *PicPath;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomClassCellIdentifer"];
id object = [self.tableData objectAtIndex:indexPath.row];
CustomClass *myObject = (CustomClass*)object;
cell.HomePicture.image = [UIImage imageNamed:myObject.PicPath];
return cell;
}
I want to mask an image like with this code:
CGFloat plx = inputImage.size.width/_imageView.frame.size.width;
CGFloat ply = inputImage.size.height/_imageView.frame.size.height;
CGFloat radius = MIN(_postionView.frame.size.width * plx, _postionView.frame.size.height * ply)/1.5;
CIVector *cen = [CIVector vectorWithX:_postionView.center.x * plx Y:_postionView.center.y * ply];
CIFilter *radialGradient = [CIFilter filterWithName:#"CIRadialGradient" keysAndValues:
#"inputRadius0", [NSNumber numberWithFloat:radius],
#"inputRadius1", [NSNumber numberWithFloat:radius + 1.0f],
#"inputColor0", [CIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0],
#"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],
#"inputCenter", cen,
nil];
CIImage *circleImage = [radialGradient valueForKey:kCIOutputImageKey];
_postionView is my uiimageview. It does not get right position. I guess this is because uikit coordinate system and quartz different coordinate system. Any ideas?
Has been resolved, the following is to solve the codeļ¼
CGPoint touchPoint = [touch locationInView:self.view];
ScaleX = _imageView.image.size.width/self.view.frame.size.width;
ScaleY = _imageView.image.size.height/self.view.frame.size.height;
CGFloat imgX;
CGFloat imgY;
imgX = touchPoint.x * ScaleX;
imgY = _imageView.image.size.height - touchPoint.y * ScaleY;
VICPoint = CGPointMake(imgX, imgY);
CIImage *maskImage = nil;
CIVector *cen;
cen = [CIVector vectorWithX:VICPoint.x Y:VICPoint.y];
CGFloat radius = MIN(80 * ScaleX, 80 * ScaleY)/1.5;
CIFilter *radialGradient = [CIFilter filterWithName:#"CIRadialGradient" keysAndValues:
#"inputRadius0", [NSNumber numberWithFloat:radius],
#"inputRadius1", [NSNumber numberWithFloat:radius + 1.0f],
#"inputColor0", [CIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0],
#"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],
#"inputCenter", cen,
nil];
CIImage *circleImage = [radialGradient valueForKey:kCIOutputImageKey];