How to Pass Multiple images to other View at a time? - ios

In my app I'm picking five images at a time and displaying in a small view and I have button if I click that button the 5 images in that view should go to next View Controller .but when I'm clicking that button only the image in 0th index is passing and remaining images are not passing.Hope someone helps me
And here is my code.This code is for passing from first view to second view and chosenImages is an array where all the picked images are saving
SecondViewController *secview=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
if (secview.imgView.tag==0) {
secview.img=[self.chosenImages objectAtIndex:0];
}
else if (secview.imgView1.tag==1)
{
secview.img=[self.chosenImages objectAtIndex:1];
}
else if (secview.imgView1.tag==2)
{
secview.img=[self.chosenImages objectAtIndex:2];
}
else if (secview.imgView1.tag==3)
{
secview.img=[self.chosenImages objectAtIndex:3];
}
else if (secview.imgView1.tag==4)
{
secview.img=[self.chosenImages objectAtIndex:4];
}
NSLog(#"%#",secview.img);
[self presentViewController:secview animated:YES completion:nil];
and in second view my code is:
if (imgView.tag==0)
{
imgView.image=img;
}
else if (imgView1.tag==1)
{
imgView1.image=img;
}
else if (imgView2.tag==2)
{
imgView2.image=img;
}
else if (imgView3.tag==3)
{
imgView3.image=img;
}
else if (imgView4.tag==4)
{
imgView4.image=img;
}
Update:In didFinishPickingMedia I wrote this code
images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info) {
if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
image=[dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
}
}
else {
NSLog(#"UIImagePickerControllerReferenceURL = %#", dict);
}
}
self.chosenImages = images;
[AllImages setHidden:NO];
previousButtonTag=1000;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 4, self.AllImages.frame.size.width, 104)];
int x =0.5;
for (int i=0; i<5; i++) {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(x, 0, 100, 123);
button.layer.borderWidth=0.5;
button.titleLabel.font=[UIFont boldSystemFontOfSize:12];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// button.layer.borderColor=[[UIColor lightGrayColor]CGColor];
button.tag = i;
[button setBackgroundImage:[self.chosenImages objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:#selector(OverLayMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[scrollView addSubview:button];
x += button.frame.size.width;
x=x+6.0;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor clearColor];
[self.AllImages addSubview:scrollView];
}

I solved my issue by declaring another array in second view.In first view we are saving picked images in one array .so that first view array objects = second view array and I passed that array in second view
my code in second view :
for (int i=0; i<[arr count]; i++) {
self.img=[arr objectAtIndex:i];
if (i==0)
{
imgView.image=img;
}
else if (i==1)
{
imgView1.image=img;
}
else if (i==2)
{
imgView2.image=img;
}
else if (i==3)
{
imgView3.image=img;
}
else if (i==4)
{
imgView4.image=img;
}
}
}
by this i'm getting correct output

I dunno that I got your question but my understanding is that you wanna send 5 images to another view. If so, define a new NSArray property such as 'images' in your SecondViewController and add it to where you go from first view to second
SecondViewController *secondVC = [SecondViewController new];
[secondVC setImages: self.chosenImages];
The problem with your code is that you're using if/else if block statement and if one of the conditions in that if/else if evaluate to true only you get into one of the block. Also, -objectAtIndex: only returns one item.
[self.chosenImages objectAtIndex:0];
Also, I'm sure that secview.img means that you have a UIImage property inside your SecondViewController.

Related

Getting frame of subview enumeration

So, I have repeating views containing thumbnails, once a thumbnail is pressed the thumbnail ID is sent as the tag.
With this information I want to get the frame of the subview of that view;
- (IBAction)thumbPressed:(id)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
for (int i = 0; i < _thumbCounter; i++) {
if (i == [sender tag]) {
NSLog(#"thumb view %i", i);
//Up To HERE
break;
}
}
// [self moveToVideoControllerWithInfoID:[NSString stringWithFormat:#"%li", (long)[sender tag]]];
}
For the thumbnails to be drawn they're drawn in a random order retrieved by JSON.
The Button
UIButton *thumbButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thumbButton addTarget:self
action:#selector(thumbPressed:)
forControlEvents:UIControlEventTouchUpInside];
thumbButton.frame = CGRectMake(0, 0, _thumbView.frame.size.width, _thumbView.frame.size.height);
thumbButton.tag = _thumbCounter;
[_thumbView addSubview:thumbButton];
The subview I want to get the frame of
NSString *string = [NSString stringWithFormat:#"***.jpg",Link];
NSURL * imageURL = [NSURL URLWithString:string];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(thumbGap, thumbGap, thumbHeight - 5, thumbHeight - 5)];
imageView.image = image;
[_thumbView addSubview:imageView];
Where the thumbnail shell is drawn
_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];
_thumbview is a class of UIVIEW with the added thumbId
How once that button is pressed can I locate the imageView frame inside of _thumbview ( bare in mind there are multiple ).
First off, let's make life easier for you:
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
ThumbView *thumbView = (ThumbView*)[sender superView];
for(UIView* subview in thumbView.subViews) {
if([subView iKindOfClass:[UIImageView class]]) {
//you got it here
}
}
}
You could shortcut the whole thing by making ThumbView have an imageView property as well.
I've tried to determine that this is what you are doing:
_scrollView
->_thumbView[0]
--->thumbButton
--->imageView
->_thumbView[1]
--->thumbButton
--->imageView
...
->_thumbView[N]
--->thumbButton
--->imageView
Assumed that you want the image view who is in the same thumbView as the button who is pressed.
Best Possible Solution (IMHO):
#Interface ThumbView()
#property (nonatomic,weak) UIImageView *imageView;
#end
AND:
(After making sure to first add, and THEN SET .imageView)
- (IBAction)thumbPressed:(UIButton*)sender {
NSLog(#"Thumb %i pressed", (int)[sender tag]);
UIImageView *imageView = ((ThumbView*)[sender superView]).imageView
}
for (UIView *i in _scrollView.subviews) {
if ([i isKindOfClass:[ThumbView class]]) {
ThumbView *tempThumb = (ThumbView *)i;
if (tempThumb.thumbId == (int)[sender tag]) {
for (UIView *y in tempThumb.subviews) {
if ([y isKindOfClass:[UIImageView class]]) {
UIImageView *tempIMG = (UIImageView *)y;
[self playVideo:[loadedInput objectForKey:#"link"] frame:tempIMG.frame andView:tempThumb];
}
}
}
}
}

Reload UIScrollView by tapping a UIButton

Let me explain my project first. I have some data in my SQLIte DB table called "note".
In "note" table I have these fields: id, noteToken, note.
What I am doing here is load all the note in an NSMUtableArray from that table. And create UIButton according to that array content number and add those buttons in a UIScrollView as subView. The number of buttons and width of scrollview generate auto according to the number of content of that array. Now, when some one tap one of those Buttons, it will bring him to a next viewController and show him the corresponding note details in that viewController.
I do the same thing with another NSMUtableArray, but these time it read all the id from the "note" table. It equally generate new delete button in the same UIScrollView. But if some one tap on these delete button it will delete that particular note from the table "note" of SQLIte DB. AND RELOAD THE UIScrollView. All are done except the RELOAD THE UIScrollView part. This is what I want. I tried with all exist solution but don't know why it's not working.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.noteToken = [NSString stringWithFormat:#"%#%#", fairId, exibitorId];
scrollViewNoteWidth = 100;
[scrollViewNote setScrollEnabled:YES];
[scrollViewNote setContentSize:CGSizeMake((noteButtonWidth * countNoteButtonArray) + scrollViewNoteWidth, 100)];
sqLite = [[SQLite alloc] init];
[self.sqLite callDataBaseAndNoteTableMethods];
self.noteButtonArrayy = [[NSMutableArray alloc] init];
noteButtonArrayy = [self.sqLite returnDataFromNoteTable:noteToken];
[self LoadNoteButtonAndDeleteButton:noteButtonArrayy];
}
//////////////*----------------------- Note Section (Down) -----------------------*//////////////
-(void) LoadNoteButtonAndDeleteButton:(NSMutableArray *) noteButtonArray
{
sQLiteClass = [[SQLiteClass alloc] init];
noteButtonArrayToShowNoteButton = [[NSMutableArray alloc] init];
/*--------------- Load the noteButton & pass note (Down)---------------*/
for (int i = 0; i < [noteButtonArray count]; i++)
{
sQLiteClass = [noteButtonArray objectAtIndex:i];
// NSString *ids = [NSString stringWithFormat:#"%d", sQLiteClass.idNum];
NSString *nt = sQLiteClass.note;
[noteButtonArrayToShowNoteButton addObject:nt];
}
[self ShowNoteButtonMethod:noteButtonArrayToShowNoteButton];
/*--------------- Load the noteButton & pass note (Up)---------------*/
/*--------------- Load the deleteButton & pass id (Down)---------------*/
noteButtonArrayToDeleteNoteButton = [[NSMutableArray alloc] init];
for (int i = 0; i < [noteButtonArray count]; i++)
{
sQLiteClass = [noteButtonArray objectAtIndex:i];
// Convert int into NSString
NSString *ids = [NSString stringWithFormat:#"%d", sQLiteClass.idNum];
[noteButtonArrayToDeleteNoteButton addObject:ids];
}
[self ShowNoteDeleteButtonMethod:noteButtonArrayToDeleteNoteButton];
/*--------------- Load the deleteButton & pass id (Down)---------------*/
}
-(void) ShowNoteButtonMethod:(NSMutableArray *) btnarray
{
countNoteButtonArray = [btnarray count];
// For note button
noteButtonWidth = 60;
noteButtonXposition = 8;
for (NSString *urls in btnarray)
{
noteButtonXposition = [self addNoteButton:noteButtonXposition AndURL:urls];
}
}
-(int) addNoteButton:(int) xposition AndURL:(NSString *) urls
{
noteButton =[ButtonClass buttonWithType:UIButtonTypeCustom];
noteButton.frame = CGRectMake(noteButtonXposition, 8.0, noteButtonWidth, 60.0);
[noteButton setImage:[UIImage imageNamed:#"note.png"] forState:UIControlStateNormal];
[noteButton addTarget:self action:#selector(tapOnNoteButton:) forControlEvents:UIControlEventTouchUpInside];
[noteButton setUrl:urls];
noteButton.backgroundColor = [UIColor clearColor];
[self.scrollViewNote addSubview:noteButton];
noteButtonXposition = noteButtonXposition + noteButtonWidth + 18;
return noteButtonXposition;
}
-(void)tapOnNoteButton:(ButtonClass*)sender
{
urlNote = sender.url;
[self performSegueWithIdentifier:#"goToNoteDetailsViewController" sender:urlNote];
}
-(void) ShowNoteDeleteButtonMethod:(NSMutableArray *) btnarray
{
countNoteButtonArray = [btnarray count];
// For delete button
deleteNoteButtonWidth = 14;
deleteNoteButtonXposition = 31;
for (NSString *idNumber in btnarray)
{
deleteNoteButtonXposition = [self addDeleteButton:deleteNoteButtonXposition AndURL:idNumber];
}
}
-(int) addDeleteButton:(int) xposition AndURL:(NSString *) idNumber
{
deleteNoteButton =[ButtonClass buttonWithType:UIButtonTypeCustom];
deleteNoteButton.frame = CGRectMake(deleteNoteButtonXposition, 74.0, deleteNoteButtonWidth, 20.0);
[deleteNoteButton setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
[deleteNoteButton addTarget:self action:#selector(tapOnDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
[deleteNoteButton setIdNum:idNumber];
deleteNoteButton.backgroundColor = [UIColor clearColor];
[self.scrollViewNote addSubview:deleteNoteButton];
deleteNoteButtonXposition = deleteNoteButtonXposition + deleteNoteButtonWidth + 65;
return deleteNoteButtonXposition;
}
-(void)tapOnDeleteButton:(ButtonClass*)sender
{
idNumb = sender.idNum;
[self.sqLite deleteData:[NSString stringWithFormat:#"DELETE FROM note WHERE id IS '%#'", idNumb]];
// NSLog(#"idNumb %#", idNumb);
//[self.view setNeedsDisplay];
//[self.view setNeedsLayout];
//[self LoadNoteButtonAndDeleteButton];
//[self viewDidLoad];
// if ([self isViewLoaded])
// {
// //self.view = Nil;
// //[self viewDidLoad];
// [self LoadNoteButtonAndDeleteButton];
// }
}
//////////////*----------------------- Note Section (Up) -----------------------*//////////////
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goToNoteDetailsViewController"])
{
NoteDetailsViewController *noteDetailsViewController = [segue destinationViewController];
[noteDetailsViewController setUrl:sender];
}
}
Here's the screen shot:
Here we can feel the difference between UIScrollView and UICollectionView, however UICollectionView is made up of UIScrollView, UICollectionView can be reload and adjust its content accordingly, where UIScrollView can't.
Ok, now in your case, you've to reload (refresh) your scroll view, which is not possible as we can with UICollectionView or UITableView.
You've two options,
Best option (little tough) : replace UIScrollView with UICollectionView - will take some of your time, but better for reducing code complexity and good performance of your app.
Poor option (easy) : Stay as it with UIScrollView - when you want to reload, delete each subview from it, and then again show and load everything. Highly not recommended.
IMHO, you should go with best option.

Is my NSMutablearray adding the objects and my NSLog the correct number of objects in the array?

Im working on a project and i need some advice. I have this button handler code that handle all my buttons clicks(like a survey). I want to click one button and have the button add the appropriate object to the NSMutable array. now my question is is it safe to use a mutable array or a dictionary since mutable objects can be added or removed. and how can i check if the objects are being added to the array correctly. i have a NSUInteger to see if the objects are being added correctly but i'm receiving in the NSlog objects in the array 1 and whenever i click another button to add another object i receive the same message but its NSlog prints 1 again so im not sure if the objects are being added correctly or not.
-(IBAction)checkBoxButtonHandler:(id)sender
{// array being allocated
NSMutableArray *arrayOfselections = [NSMutableArray array];
if (sender == checkButton1)
{
if (!buttonChecked)
{
[arrayOfselections addObject:#"Freshman"];
NSLog(#"Freshman Checked");
[checkButton1 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkButton1 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
if (sender == checkB2)
{
// [arrayOfselections addObject:#"Sophmore"];
// NSLog(#"check 2");
if (!buttonChecked)
{
[arrayOfselections addObject:#"Sophmore"];
NSLog(#"Sophmore Checked");
[checkB2 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkB2 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
[arrayOfselections accessibilityElementCount];
if (sender == checkBox3)
{
//NSLog(#"check 3");
if (!buttonChecked)
{
[arrayOfselections addObject:#"Junior"];
NSLog(#"Junior Checked");
[checkBox3 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkBox3 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
if (sender == checkBx4)
{
NSLog(#"Checked 4");
if (!buttonChecked)
{
[arrayOfselections addObject:#"Senior"];
NSLog(#"Senior Checked");
[checkBx4 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkBx4 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
NSUInteger arraySize = [arrayOfselections count];
NSLog(#"Number of stuff inside array: %ui",arraySize);
}
You create new array every time you press the button, this happened in this line:
NSMutableArray *arrayOfselections = [NSMutableArray array];
You need to create it just once. The best way is just move it to viewDidLoad method, it should do the job.
You need to make the array a property of the view controller:
MyViewController.m:
#interface MyViewController ()
#property (nonatomic) NSMutableArray *arrayOfSelections;
#end
Which is initialized in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.arrayOfSelections = [NSMutableArray new];
...
}
-(IBAction)checkBoxButtonHandler:(id)sender
{
// Remove this line:
//NSMutableArray *arrayOfselections = [NSMutableArray array];
...
// and change any references to self.arrayOfSelections:
[self.arrayOfselections addObject:#"Freshman"];
}
A prettier design for this would be to use tags and a dictionary instead of if-else, so
// assign tags:
checkButton1.tag = 1;
checkButton2.tag = 2;
// etc., then setup data beforehand
NSDictionary *mapTags = #{ #1:#"Freshman", #2:#"Sophomore" /* etc */ };
NSMutableArray *arrayOfselections = [NSMutableArray array];
// then look how small this gets:
-(IBAction)checkBoxButtonHandler:(id)sender {
NSNumber *tagNumber = [NSNumber numberWithInt:sender.tag];
[self.arrayOfselections addObject:self.mapTags[tagNumber]];
}

NSDictionary store the objects from the last array

I have created 3 buttons at my top.take the offset of them and giving them same IBAction.
I want to change images at below according to the which button is pressed.
Further i have to pass these images to the another ViewControler, Here
i have got 3 arrays named bundle1 , bundle2 , bundle3 . in each array i have set of 5 images, in my Viewdidload as below,
bundle1 = #[#"1.png",#"2.png", #"3.png", #"4.png" ,#"5.png"];
bundle2 = #[#"6.png", #"7.png", #"8.gif",#"9.jpeg", #"10.jpeg" ];
bundle3 = #[#"11.jpg", #"12.jpg" ,#"13.jpeg", #"14.jpeg", #"15.jpeg"];
now i had take mutabledictionary, which had upper 3 arrays and i had give them a specific key
as below.
mutdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: bundle1 , #"key1" , bundle2 , #"key2" , bundle3 , #"key3" ,nil];
Now below is the code for IBAction of Upper 3 buttons, (Which is the same for all 3 buttons).
-(IBAction)morebtn:(id)sender
{
for (UIView *subview in [scroll subviews])
{
[subview removeFromSuperview];
}
arraydata = [[NSMutableArray alloc] init];
if ([sender tag] == 1) {
NSLog(#"hey btn1 clicked");
for (UIImage *b in arraydata)
{
[arraydata removeAllObjects];
}
arraydata = [mutdict valueForKey:#"key1"];
}
if ([sender tag] == 2) {
NSLog(#"hey btn2 clicked");
for (UIImage*b in arraydata)
{
[arraydata removeAllObjects];
}
arraydata = [mutdict valueForKey:#"key2"];
}
if ([sender tag] == 3) {
NSLog(#"hey btn3 clicked");
for (UIImage*b in arraydata)
{
[arraydata removeAllObjects];
}
arraydata = [mutdict valueForKey:#"key3"];
}
int x=10,y=300;
for (int k=0; k<arraydata.count; k++) {
NSLog(#"main called");
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:[arraydata objectAtIndex:k]] forState:UIControlStateNormal];
btn.tag=k;
btn.frame=CGRectMake(x, y, 100, 100);
[btn addTarget:self action:#selector(btn4images1:) forControlEvents:UIControlEventTouchUpInside];
x+=100;
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 200)];
scroll.contentSize = CGSizeMake(self.view.frame.size.width , 200);
[scroll addSubview:btn];
[self.view addSubview:scroll];
}
}
in upper code i trace which button is pressed and according to it i wil give inages in my NSArray named arraydata.
when i first time click on the all 3 upper buttons they show me image related to their array, but when i second time click on button1 (placed on the top), The images related to button3 are also shown in the Buttonbackground (palced at below part).
so on clicking button1 in below part button background images are overlaped. Seems like my NSMutableDictionary stores the data of last array. don't know what exactly happen.

UIImage animation not working for all Button

I have this simple function where i sap rate the button animation and then start animation for every button but i dont know why only one button animate not others which clicked, please help me with this
- (IBAction)startAnimation:(UIButton *)button {
NSMutableArray* imagesArray = [[NSMutableArray alloc] init];
for (int images = 0; images < 15; images++) {
UIImage* buttonImage = [UIImage imageNamed:[NSString stringWithFormat:#"aaqqq00%02d.png", images]];
[imagesArray addObject:buttonImage];
}
NSArray* reversedAnim = [[imagesArray reverseObjectEnumerator] allObjects];
int buttonTag = button.tag;
animButton.adjustsImageWhenHighlighted = NO;
for (int images = 0; images < 15; images++) {
UIButton *animButton = (UIButton *)[self.view viewWithTag:buttonTag];
if (images <= buttonTag) {
animButton.imageView.animationImages = imagesArray;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0014.png"] forState:UIControlStateNormal];
} else {
animButton.imageView.animationImages = reversedAnim;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0000.png"] forState:UIControlStateSelected];
}
NSLog(#"%#", animButton.imageView.animationImages);
animButton.imageView.animationDuration = 1; //whatever you want (in seconds)
animButton.imageView.animationRepeatCount = 1;
[animButton.imageView startAnimating];
}
}
You're passing the button, so doing the tag lookup seem pointless and error prone (if there are multiple views with the same tag in the hierarchy).
Setting the buttons image view animation images (animButton.imageView.animationImages) and then after that setting a single image ([animButton setImage:...) is also pointless.
What does your log statement say about the animation images?

Resources