How to create .rtf and edit in iOS? - iphone

I have two UIImageView and two textfiled under the image view. One textfield for image1 and second textfield for image2.
Now I want that user enter text and give image name and that image will save in document directory in .rtf format. And the when I open .rtf I will be able to edit that rtf.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,50, 100, 100)];
[imageView setImage:[UIImage imageNamed:#"image1.png"]];
self.view addSubview:imageView];
[imageView release];
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100,150, 100, 100)];
[imageView1 setImage:[UIImage imageNamed:#"image2.png"]];
self.view imageView1];
[imageView1 release];
txt=[[UITextField alloc]initWithFrame:CGRectMake(5,190, 180, 30)];
txt.backgroundColor=[UIColor whiteColor];
txt.placeholder=#"Enter Text";
txt.textColor = [UIColor blackColor];
txt.delegate =self;
[txt addTarget:self action:#selector(ImageSave_method) forControlEvents:UIControlEventEditingDidEnd];
txt.textAlignment=NSTextAlignmentCenter;
txt.userInteractionEnabled = YES;
txt.autocapitalizationType = NO;
txt.font=[UIFont fontWithName:#"Helvetica" size:15];
[self.view addSubview:txt];
txt2=[[UITextField alloc]initWithFrame:CGRectMake(200,190, 180, 30)];
txt2.backgroundColor=[UIColor whiteColor];
txt2.placeholder=#"Enter Text";
txt2.textColor = [UIColor blackColor];
txt2.delegate =self;
[txt2 addTarget:self action:#selector(ImageSave_method) forControlEvents:UIControlEventEditingDidEnd];
txt2.textAlignment=NSTextAlignmentCenter;
txt2.userInteractionEnabled = YES;
txt2.autocapitalizationType = NO;
txt2.font=[UIFont fontWithName:#"Helvetica" size:15];
[self.view addSubview:txt2];
I have no idea how to do this.
Any Idea or suggestion would be highly welcome.

you can use the nsuserdefaults to saved the image name
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:_imageName1 forKey:#"imageName1"];
[userDefault setObject: _imageName2 forKey:#"imageName2"];
[userDefault synchronize];
for retrieving the data from nsuserdefaults
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSString *_imageName1 = [userDefault objectForKey:#"imageName1"];
NSString *_imageName2 = [userDefault objectForKey:#"imageName2"];
to save the image in documents directory
create the file path
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath1 = [NSString stringWithFormat:#"%#/imageName1.png",
docDir];
convert the UIImage into nsdata
NSData *data = UIImagePNGRepresentation(myImage.image);
then save the image into imagePath1 of document directory
[data writeToFile:imagePath1 atomically:YES];
for retrieving the image
UIImage *img = [UIImage imageWithContentsOfFile:imagePath1];

Related

Copy From One NSArray to another when generate the Randomly images from First Array

I am developing one app in which loading images from NSMutableArray. which generate the random images and load it to the UIImageview in iOS. I want to save that array after loading as it is into Another array how may I do this. Thanks in advance.
My Code As Below.
//FrontsCards is My Array
for(m=0; m<[FrontsCards count];m++)
{
ImgView.alpha=1;
ImgView.tag=m;
NSLog(#"Img View Tag %d",ImgView.tag);
randIdx=arc4random()%[FrontsCards count];
NSString *imageName=[FrontsCards objectAtIndex:randIdx];
NSString *fullImageName=[NSString stringWithFormat:#"%#",imageName];
int padding=0;
CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
[ImgView setImage:[UIImage imageNamed:fullImageName]];
[scrollView addSubview:ImgView];
[ImgView setAccessibilityIdentifier:[FrontsCards objectAtIndex:randIdx]];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapImgView:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[self.ImgView setImage:[UIImage imageNamed:fullImageName]];
NSLog(#"full image name %#",fullImageName);
NSLog(#"Image Name = %#", fullImageName);
[self.ImgView setTag:randIdx];
[self.ImgView addGestureRecognizer:doubleTap];
self.ImgView.userInteractionEnabled=YES;
}
CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
[scrollView setContentSize:scrollViewSize];
[self.view addSubview:scrollView];
Create Another Array and init it ViewDidLoad and add Objects one by one as they generated.
NSString *imageName=[FrontsCards objectAtIndex:randIdx];
[yourNewArray addObject:imageName];
Try this snippet to copy an NSMutableArray
NSArray *newArray = [[oldArray mutableCopy]
OR:
NSMutableArray *newArray = [[[NSMutableArray alloc] initWithArray:oldArray copyItems:YES] autorelease];
Just after this code :
randIdx=arc4random()%[FrontsCards count];
save the index into another array and use it to point to the original array .

How to load Images from NSMutableArray in UITableviewCell in iPhone?

I need to laod a image from NSMutableArray which is loaded from service url.I can retrieve the image tag from XML response.Now I want that image in a UITableViewCell
My array looks like this :
(
{
Image="46b6daca-3.png";
}
{
Image="9251bfe5-d.jpg";
}
)
How can I load each image in a seperate UITableViewCell ?
For creating a UILabel I knew it is like this :
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
line1 .font=[UIFont fontWithName:#"arial" size:12];
[line1 setTextAlignment:UITextAlignmentLeft];
[line1 setText:[d valueForKey:#"Name"]];
line1.tag=113;
[cell addSubview:line1 ];
[line1 release];
How can I do it for UIImage?
UIImageView *img = [[UIImageView alloc]initWithImage:[ d objectForKey:#"Image"]];
[cell.contentView addSubView:img];
as per you code.. try this.
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
UIImageView *img = [[UIImageView alloc]initWithImage:[ d objectForKey:#"Image"]];
img,frame = CGRectMake("Set frame")]
[cell.contentView addSubView:img];
[img release];
if you have images in your array then use direct array value as a image like bellow..
UIImageView *img = [[UIImageView alloc]initWithImage:[[arrData valueForKey:#"Image"] objectAtIndex:indexPath.row];
img.frame = SetFrameHere;//
[cell.contentView addSubView:img];
or also add as a cell imageview like bellow..
cell.imageView.image = (UIImage *)[[arrData valueForKey:#"Image"] objectAtIndex:indexPath.row];
Use the following code for setting the image:
UIImage *img = [[UIImage imageNamed:[d objectForKey:#"Image"];
cell.imageView.image = img;
Here d is your NSMutableDictionary

Load qr code from url and display as image view

As the title says I'm trying to take a qr code like this - http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=044779-A55W6UZD&chld=H|0 and display it as a UIImageview. I've searched but couldn't find an answer anywhere. Please help.
I've tried this but it doesn't seem to work
NSData *receivedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc] initWithData:receivedData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.frame = CGRectMake(20, 160, 200, 200);
[self.view addSubview:myImageView];
Try encoding the url like this and then use encodedUrl in the rest of the code:
NSString* encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
You should also remember to release the objects you created, otherwise you leak memory.
NSData* receivedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:encodedUrl]];
UIImage* image = [[UIImage alloc] initWithData:receivedData];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.frame = CGRectMake(20, 160, image.size.width, image.size.height;
[self.view addSubview:myImageView];
[receivedData release];
[image release];
[myImageView release];

How to programmatically set the background on a UICustomButton?

I know this question has been asked and answered in other forums, but I can't seem to duplicate what they did and get it to work. I am trying to set the background to a button using a picture that I download from a server. Everything works until I try to make a NSData object with the contents of a URL. When I try and print the Data to the screen it comes back Null. What am I doing wrong?
myImageURLString = [[NSMutableString alloc] initWithString:#"http://destinationnexus.com"];
keyName = [businessButtonTagArray objectAtIndex:i];
arrayFromBusinessDictionary = [businessDictionary objectForKey:keyName];
stringToAddToMyImageURLString = [arrayFromBusinessDictionary objectAtIndex:1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:i];
button.frame = CGRectMake(xOffSet+(countForX*xMult), yOffset +(countForY*yMult), 215, 155);
[myImageURLString appendString:stringToAddToMyImageURLString];
NSLog(#"Appended URL String: = %#", myImageURLString);
NSURL *myImageURL = [NSURL URLWithString:#"http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729//.jpg"];
NSLog(#"NSURL = %#", myImageURL);
//THIS IS WHERE I SEEM TO HAVE A PROBLEM.....
NSData *imageData = [[NSData alloc] initWithContentsOfURL:myImageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData] ;
NSLog(#"Image Data = %#", imageData);
//UIImage *myImage = [UIImage imageWithData:imageData] ;
[button setBackgroundImage:image forState:UIControlStateNormal];
When I run the program this is what it outputs in the Log:
Appended URL String: = http://destinationnexus.com/pictures/directory/Auburn-University-Hotel-and-Center-in-Auburn-Alabama-36830-5429.jpg
NSURL = http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg
Image Data = (null)
You should correct your myImageURL, by deleting 2 slashes at the end of URL.
The code goes something like this.
NSURL *myImageURL = [NSURL URLWithString:#"http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:myImageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData] ;
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[imageData release];

iPhone: how to dealloc custom cell labels

hi i am working on UITableView custom cell for twitter, for each cell i am adding UIImageView(userPhoto), UILabel(name), UILabel(tweet), UILabel(Data), This i have to dealloc all elements,
please go through the attached Image of INSTRUMENT TOOL alloc screen shot
when i run the Instrument tool, it showing some are not dealloc, how to dealloc,
this is my code
//Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil];
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(72,3,242,15)] ;
name.text = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] userName];
[name setFont:[UIFont fontWithName:#"Helvetica" size:13]];
name.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];
[name setBackgroundColor:[UIColor clearColor]];
UILabel *date = [[[UILabel alloc]initWithFrame:CGRectMake(200,3,130,15)] autorelease];
date.text = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] created_at];
[date setFont:[UIFont fontWithName:#"Helvetica" size:12]];
date.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];
[date setBackgroundColor:[UIColor clearColor]];
UILabel * tweetLabel = [[UILabel alloc]initWithFrame:CGRectMake(72,20,242,60)] ;
tweetLabel.text = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] tweet];
tweetLabel.numberOfLines = 3;
tweetLabel.textColor=[UIColor colorWithRed:252 green:148 blue:31 alpha:1];
[tweetLabel setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[tweetLabel setBackgroundColor:[UIColor clearColor]];
UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,10,58,60)] ;
NSURL *url = [NSURL URLWithString:[(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]];
NSData *data = [NSData dataWithContentsOfURL:url];
[myImage setImage: [UIImage imageWithData:data]];
[cell.contentView addSubview:myImage];
[cell.contentView addSubview:tweetLabel];
[cell.contentView addSubview:name];
[cell.contentView addSubview:date];
[myTweetActivityIndicator stopAnimating];
[myTweetActivityIndicator setHidesWhenStopped:YES];
return cell;
}
please guide me
Thank you
You can use
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil] autorelease];
For other labels, you can probably use
[date release]; [tweetLabel release]; [name release]; [myImage release];
You'll have to send a release message to all the objects that you have allocated using init/alloc (name, date, tweetLabel, myImage) as soon as you're finished with them (for example, after addSubview). Note that according to the documentation, the view is retained by the receiver.
try using
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
[myImage setImage: [UIImage imageWithData:data]];
[data release];
data = nil;
In this case, we are manually releasing the memory allocated. I hope, this works