Add multiple UIImage copies using UIStepper - ios

I want to add/delete images in a view using a UIStepper. So every time you press the '+' the same image is copied within the same viewController, still being able to move/rotate every copy.
I have something like this:
- (IBAction)ValueChanged:(UIStepper *)value
{
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
int j = (int) value.value;
for( int i = 0 ; i < j ; ++i )
{
UIImageView *newImageView = [[UIImageView alloc] init];
newImageView.image = self.myImageView.image;
[imageArray addObject:newImageView];
[self.view addSubview:imageArray[i]];
}
}
but this does not work because it just replaces the previous image.
Is there a proper way to copy the same image multiple times using the UIStepper?

Related

Enlarging image when tapped ios

I have an app that implements horizontal gallery. In my story board when a job (cell in a tableview) is tapped it will redirect and display job details. In my Job Details Scene : I have index 0 : for company logo and name, salary and the position. in index 1 : i have the horizontal gallery - I created a scrollview programmatically and created a imageviews inside the scrollview.
Here's how I created the scrollview and imageviews:
else if (indexPath.section == 1)
{
[self getImages];
[cell.contentView addSubview:self.scrollView];
return cell;
}
- (void)getImages
{
mutableURLstorage = [[NSMutableArray alloc] init];
imagesArray = [[NSMutableArray alloc]init];
NSInteger noOfImages = (unsigned long)[mutableStorage count];
NSString* strURL;
//store image_paths (company and branch) in a mutable array
for (int i = 0; i < noOfImages; i++)
{
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [NSDate date];
NSDate *oneMinLater = [date dateByAddingTimeInterval:1444];
NSString *hmac_body =[NSString stringWithFormat:#"%#\n%lld\n%#",METHOD,[#(floor([oneMinLater timeIntervalSince1970])) longLongValue],CONTAINER_PATH] ;
// below get different string with shaA diagest
const char *cKey2 = [KEY cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData2 = [hmac_body cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC2[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey2, strlen(cKey2), cData2, strlen(cData2), cHMAC2);
NSData *HMAC2 = [[NSData alloc] initWithBytes:cHMAC2 length:sizeof(cHMAC2)];
NSString *HMACStr = [[HMAC2 description] stringByReplacingOccurrencesOfString:#"<" withString:#""];
HMACStr = [HMACStr stringByReplacingOccurrencesOfString:#">" withString:#""];
HMACStr = [HMACStr stringByReplacingOccurrencesOfString:#" " withString:#""];
strURL = [NSString stringWithFormat:#"%#%#?temp_url_sig=%#&temp_url_expires=%lld", URLstorage, CONTAINER_PATH, HMACStr, [#(floor([oneMinLater timeIntervalSince1970])) longLongValue]];
[mutableURLstorage addObject:strURL];
[imagesArray addObject:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]]];
}
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGSize pageSize = CGSizeMake(ITEM_WIDTH, self.scrollView.frame.size.height);
_pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenRect.size.height-30, 320, 36)];
_pgCtr.backgroundColor=[UIColor grayColor];
int numberofPage=ceil((float)[imagesArray count]/3.5);
_pgCtr.numberOfPages= numberofPage;
self.scrollView.contentSize = CGSizeMake(320*numberofPage, pageSize.height);
int imgCurrentX = 10;
for (UIImageView* image in imagesArray) {
#autoreleasepool {
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(imgCurrentX, 0, 70, 70)];
imageview.image = (UIImage*)image;
[imageview setUserInteractionEnabled:YES];
[self.scrollView addSubview:imageview];
imgCurrentX = imgCurrentX+80;
}
}
}
I am able to display the horizontal gallery but the images are all for thumbnails.
Now I implemented this code in my viewDidLoad to determine whether an imageview is tapped:
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapAction:)]];
In my singleTapAction method i am using LeveyPopListView to display the enlarged iamges.
- (void)singleTapAction:(UIGestureRecognizer *)singleTap_
{
[self createLeveyPopList];
}
- (void)createLeveyPopList
{
NSString *jobs_name = #"JOBSD";
if(self.lplv.delegate != nil)
return;
self.lplv = [[LeveyPopListView alloc] initWithTitle:#"The following jobs are located here:" options:imagesArray jobName:jobs_name handler:^(NSInteger anIndex) {
}];
self.lplv.delegate = self;
[self.lplv showInView:self.view animated:YES];
}
What I did in my pop up is redisplay the original image gallery (the one in job details scene) but the images are enlarged. My app is successfully implementing the horizontal image gallery. Now my problem is, for example image 2 is tapped, I need to display the image 2 first instead of displaying image 1 first.
I hope someone can help me with this. Thank you!
Add a method to change the size of your ImageView that contains the image, like so:
- (IBAction)buttonTapped:(id)sender {
// Use this Sender property to your advantage
[yourImageViewName setFrame:CGRectMake(0, 0, 100, 100)];
}
Of course, adjust the coordinates to however you would like them. You can also play around in the image view's settings in IB to make sure that your picture does not become distorted upon enlargement.
Now, you could also add tags to your images and enlarge the image view based upon which tag you've pressed. If you want to go that route instead, let me know and I'll post the code. I hope I helped!

Implementing a volume graph on iOS

I'm trying to implement a graph (UIView) like this:
My current approach is to draw the rounded rects with UIBezierPath inside drawRect, store a pointer to every path in a private NSMutableArray and in my View Controller send a message to update the graph with the new value. (https://gist.github.com/gverri/f238ad17f90013bfc832)
But somehow I can't send a message to my view to change the colors of the paths.
.
I'm still a relative beginner and I'm sure I'm trying to reinvent the wheel somehow. Or just missing something obvious.
Should I redraw the graph at every update (1s ~ 2s)?! Should I use a special framework to do this?
Edit:
After digging a little I found out that I can't save a pointer to a path. After drawRect my pointers disappear from the array.
It seems I would need to use Core Graphics do make it work with this approach.
Is there an easier alternative?
I made a test app that uses a column view with 7 rounded rect subviews to implement the graph. Here's the ColumnView class (a subview of UIView),
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSMutableArray *temp = [NSMutableArray new];
_offColor = [UIColor colorWithRed:238/255.0 green:230/255.0 blue:238/255.0 alpha:1];
_onColor = [UIColor colorWithRed:154/255.0 green:102/255.0 blue:155/255.0 alpha:1];
for (int i=0; i<7; i++) {
UIView *roundRect = [[UIView alloc] initWithFrame:CGRectMake(0, i*34, frame.size.width -2, (frame.size.height/7) - 4)];
roundRect.backgroundColor = _offColor;
roundRect.layer.cornerRadius = 3;
[temp insertObject:roundRect atIndex:0];
[self addSubview:roundRect];
}
_columns = [NSArray arrayWithArray:temp];
}
return self;
}
-(void)setNumberOfDarkRects:(NSInteger) num {
[self.columns enumerateObjectsUsingBlock:^(UIView *sub, NSUInteger idx, BOOL *stop) {
if (idx + 1 <= num) {
sub.backgroundColor = self.onColor;
}else{
sub.backgroundColor = self.offColor;
}
}];
}
This is how I create the grid, and test updating the columns,
#interface ViewController ()
#property (strong,nonatomic) NSMutableArray *columns;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.columns = [NSMutableArray new];
for (int i=0; i<21; i++) {
ColumnView *column = [[ColumnView alloc] initWithFrame:CGRectMake(i * 22 + 10, 50, 22, 240)];
[self.view addSubview:column];
[self.columns addObject:column];
}
[self performSelector:#selector(doRandomColoring) withObject:nil afterDelay:2];
}
-(void)doRandomColoring {
for (ColumnView *col in self.columns) {
[col setNumberOfDarkRects:arc4random_uniform(8)];
}
[self performSelector:#selector(doRandomColoring) withObject:nil afterDelay:.2];
}

How to assign something to a variably named UIImageView?

I have 5 objects that i want to assign to UIImageViews named "obstacle1","obstacle2",etc. how can i do this in a for loop somewhat like this...
for(int i=0;i<5;i++)
{
UIImageView "obstacle%d",i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"square.png"]];
}
//the <"obstacle%d",i> part is what i need help with
You should really use an Array for holding your UIImageView objects:
NSMutableArray *imageViews = [NSMutableArray array];
for(int i=0;i<5;i++)
{
UIImageView *anObstacle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"square%d.png",colorPick]]];
[imageViews addObject:anObstacle];
}
Then you can just access them in the array by doing imageViews[0].

Changing Image When Button Is Pressed - iOS - Xcode

I am trying to change an image to another random image when a button is pressed.
I have uploaded 5 images to my "Supporting Files" and I now need to reference them.
I need to create an array which holds all of the images.
I create a button that will run a method when is clicked.
This method will choose a random number of the array and then display that image.
Can someone please give me some sample code for the 3 things I need to do.
(Please note I will also be needing to display text underneath the images in a label field and it will have to be linked to the image being displayed)
Thanks
take an array like this in viewDidLoad method,take three global variables imagesArray,titlesArray and
imageview
imagesArray = [NSArray alloc]initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png" nil];
titlesArray = [NSArray alloc]initWithObjects:#"title1",#"title2",#"title3",#"title4",#"title5" nil];
-(IBAction)randomImage:(id)sender{
int n= arc4random() % [imagesArray count];
imageview.image= [UIImage imageNamed:[imagesArray objectAtIndex:n]];
label.text=[titlesArray objectAtIndex:n];
}
Try this
NSMutableArray * imagenames = [[NSMutableArray alloc] initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png", nil];
NSMutableArray * Images = [[NSMutableArray alloc] init];
for(int i=0;i<imagenames.count;i++)
{
UIImage * image = [UIImage imageNamed:[imagenames objectAtIndex:i]];
[Images addObject:image];
}
int n= arc4random() % 5;
if(n<Images.count)
{
UIImageView * imageView = [[UIImageView alloc] initWithImage:[Images objectAtIndex:n]];
}

UItableView with multiple images and setting onclick on each one of them

I have a scenario where I want to keep "thumbnails" of images on the row of the tableview. I decide on runtime the numbers of images that goes in a row.
Secondly, On click of any of the image I want to launch a viewcontroller with elaborated image of it with some description.
How can I achieve this?
|________________|
| 1 2 3 4 5 6 |
|________________|
| 7 8 9 10 11 12 |
|________________|
| 12 14 15 |
|________________|
Imagine the numbers above as thumbnails. Onclick of any of the number I want to launch a new view controller which gives details about the image.
code snippet:
-(void) populateTableView
{
NSMutableArray* srcArray = [[NSMutableArray alloc] initWithArray:[[ImageDataSource sharedImageDataSource] getThumbnailsSrcArray]];
int noOfImages = srcArray.count;
float rowc = (noOfImages / ROW_ELEM_COUNT) + 0.5;
int rowCount = roundf(rowc);
titleData = [[NSMutableArray alloc]init];
int j=0;
for (int i=0; i<rowCount; i++) {
NSMutableArray* rowArray = [[NSMutableArray alloc] init];
for (int k=0; k < ROW_ELEM_COUNT; k++) {
if (j < noOfImages) {
NSString* imgPath = [srcArray objectAtIndex:j];
UIImage* img = [[UIImage alloc] initWithContentsOfFile:imgPath];
[rowArray addObject:img];
[img release];
imgPath=nil;
}
j++;
}
[titleData addObject:rowArray];
}
titleView = [[UITableView alloc] initWithFrame:CGRectMake(90.0,156.0,590.0,630.0)
style:UITableViewStyleGrouped];
titleView.delegate = self;
[self.view addSubview:titleView];
[titleView release];
}
So basically I have array of array as my DS. index of each array will be row of table view and the array inside will have images. But I am not sure how to populate the tableview.
Any clue anybody?
You have two choices.
• Add a UIButton with the UIImage and add each button as a subview on your tableview cell.
or
• Figure out where on the cell the user tapped and from here calculate which image they tapped and which action to take:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
// do your stuff
//..
}

Resources