How to stop saving image. which is already exist in iphone photo library.
This code is for savingImage.....
-(IBAction)saveImage:(id)sender
{
NSLog(#"calling save");
if (coverPage !=nil)
{
NSData * imageData = UIImagePNGRepresentation(coverPage);
UIImage *theImage = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
}
else {
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Image is nil" delegate:self cancelButtonTitle:#"Cancle" otherButtonTitles:#"OK",nil];
[alertView show];
[alertView release];
}
}
if image is saved and clicking again and again the photo is saving..... How to stop saving image once it saved.
Without having more context it is difficult to say, but broadly speaking you can use a variable to track whether or not you have previously saved the current coverPage and simply avoid re-saving it if you have. For example:
-(IBAction)saveImage:(id)sender{
NSLog(#"calling save");
if (coverPageAlreadySaved) {
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Silly user, you already saved this image." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[alertView show];
[alertView release];
}
else if (coverPage !=nil) {
NSData * imageData = UIImagePNGRepresentation(coverPage);
UIImage *theImage = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
coverPageAlreadySaved = YES;
}
else {
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Image is nil" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[alertView show];
[alertView release];
}
}
...just be sure to set coverPageAlreadySaved = NO whenever the coverPage is changed (wherever you happen to be doing that in your code).
Alternately, since you already have the check against nil, you could just do:
-(IBAction)saveImage:(id)sender{
NSLog(#"calling save");
if (coverPage !=nil) {
NSData * imageData = UIImagePNGRepresentation(coverPage);
UIImage *theImage = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
coverPage = nil;
}
else {
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Image is nil" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[alertView show];
[alertView release];
}
}
...which will prevent the image from being re-saved until it gets updated somewhere else in your code (at which point I assume it counts as a new coverPage).
If it's just one image you're saving then you can perform this just with a BOOL ivar for flag, like:
BOOL imageSaved;
And whenever you tap the button it'll just check if !imageSaved then perform the saving.
But if it's more then one image then you can do this using NSMutableArray.Like:
NSMutableArray *savedImages;//it's an ivar
Then before performing the saving operation:
NSString *imageName=......//Here I asssume you can get the image's name
if(!imageSaved)
NSMutableArray *savedImages=[[NSMutableArray alloc]init];
if (![savedImages containsObject:imageName]){
.........here comes the saving operation
and at the end you add the imageName to the array
[savedimages addObject:imageName];
}
I've given you just an example. You might not know the names of the images you save. If that's the case you can mark the image (which can be performed in several ways) and add the image itself.
Set the UIButton as an IBOutlet as well. Then set the enabled property to NO in the IBAction. Set the UIButton IBOutlet back to YES somewhere else in your program to 'reset' the button. It is similar to using a UIStepper in that you have to declare it twice...once as an IBAction and once as an IBOutlet.
This will allow you to use the button once, then it is 'greyed out' or 'muted' to prevent further activation. Basically you are disabling the UIGestureRecognizer from the UIButton.
Set the IBAction and IBOutlet.
- (IBAction)saveImageButton:(id)sender;
#property (weak, nonatomic) IBOutlet UIButton *saveImageButton;
Set the enabled to NO in the IBAction.
- (IBAction)saveImageButton:(id)sender {
UIImageWriteToSavedPhotosAlbum(_processedImageView.image, nil, nil, nil);
_saveImageButton.enabled = NO;
}
Then change it to YES somewhere else in your program to 'reset' the button so you can use it again.
_saveImageButton.enabled = YES;
Related
I have taken an NSString from one class to another to be saved on parse.com
I have a quiz view controller where a user enters a question in a UITextView and then they press next to go to the select friends view controller where they select a user and then send the question over parse.
quiz.h
#property (nonatomic,strong) IBOutlet UITextView *textField;
#property (nonatomic, strong) NSString *text;
quiz.m
- (void)viewDidLoad {
[super viewDidLoad];
UITextView *textView = [[UITextView alloc] init];
NSString *text = self.textField.text;
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = text;
}
- (IBAction)next:(id)sender {
if ([text length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error!"
message:#"Enter some text"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
} else {
}
}
selectfriendsviewcontroller.h
#property (nonatomic, strong) NSString *string;
selectfriendsviewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
quizViewController *qvc = [[quizViewController alloc] init];
qvc.text = string;
UITextView *textfield = [[UITextView alloc] init];
string = textfield.text;
NSLog(#"%#", self.string);
}
if i remove everything except the NSLog in the selectfriends.m file in viewdidload method, the file wont upload to parse. and i get the alertView showing and error saying not file or directory exists.
then i upload to parse.com
- (void) uploadMessage;
{
NSString *text =string;
NSString *fileType= #"text";
NSString * fileName= #"text.txt";
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
PFFile *file = [PFFile fileWithName:fileName data:data];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please try sending your message again" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
1) the string does not pass as null as null does not show in the console at runtime. (nothing shows in the console even with the NSLog statement)
2) the file uploads to parse.com as a text.txt file but when i download the file to check the text the file is blank.
So in quizVC type a question then press next to go to selectfirendsVC then select friends and upload to parse.com
how can i upload the textfile to show the user inputted text in the UITextView in the quizVC?
You don't want to communicate with just any instance of your SelectFriends vc, like the one created in your viewDidLoad. You must communicate with the instance that is going to be presented.
Remove the code in Quiz vc's viewDidLoad. Create a segue from Quiz vc to SelectFriends vc. In Quiz vc, implement prepareForSegue. In that method, interrogate the textView's text and set the string property on the segue.destinationViewController. That's the one that will be presented.
(A more general treatment of vc-vc communication here).
Currently my app gives the option to save to device, or email, the latter attaching the image automatically to the mail, I am looking to add a post to twitter option, simply attaching the image and posting to Twitter, I have done this a few times with other apps, but cannot seem to get this one working.
Here is the process for email;
-(void)displayComposerSheet
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#""];
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/email.png"];
[mSplashView SaveResultImage:path];
[mail addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:#"image/png" fileName:#"Attached image"];
NSString *msg = [NSString stringWithFormat:#"I made this image!", [UIDevice currentDevice].model];
NSString* mailcontent = [NSString stringWithFormat:#"<br> %# <br>", msg];
[mail setMessageBody:mailcontent isHTML:YES];
[self presentModalViewController:mail animated:YES];
[mail release];
}
I am struggling to see how I can use similar to attach the image to Twitter, I currently use this code, but it crashes when attempting to post;
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[self presentViewController:twitter animated:YES completion:nil];
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/email.png"];
[mSplashView SaveResultImage:path];
[twitter setInitialText:#"I made this image!"];
[twitter addURL:[NSData dataWithContentsOfFile:path]];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Thank you" message:#"Posted successfully to Twitter." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
if(res == TWTweetComposeViewControllerResultCancelled) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Cancelled" message:#"You Cancelled posting the Tweet." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
[self dismissViewControllerAnimated:YES completion:nil];
};
}
Normally I could simply call [twitter addImage:]; but unfortunately it seems the image is not grabbed correctly without going through the processes above in the mail sheet.
You can't pass NSData for -addURL: method.
If your image store on disk, you can create image with [UIImage imageWithContentsOfFile:imagePath] method. Next add it with -addImage:
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/imgtweet.png"];
[UIImagePNGRepresentation(imageView.image) writeToFile:path atomically:YES];
//NSLog(#"path %#",path);
UIImage *new_image = [UIImage imageWithContentsOfFile:path];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:self.txtContent.text];
[tweetSheet addImage:new_image];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
Using Previous Answer,Thanks :)
Happy coding :)
I'm new with cocos2d so i've got a problem with my In-App Purchase Class Helper. I wrote game in Cocoa Touch and this class working perfectly, but i'm writing now the same game in Cocos2d and problem is with NSString.
Here are some snippets.
This method is called as first when i clicked some button. As you see completeIdentifier is simple string with bundleIdentifier and some string parameter. It's ok, in this place i can log this completeIdentifier.
- (void)prepareToPurchaseItemWithIdentifier:(NSString *)aIdentifier showAlertWithTitle:(NSString *)title description:(NSString *)description delegate:(id)aDelegate{
self.delegate = aDelegate;
identifier = aIdentifier;
completeIdentifier = [NSString stringWithFormat:#"%#.%#", [[NSBundle mainBundle] bundleIdentifier], aIdentifier];
askToPurchase = [[UIAlertView alloc] initWithTitle:title message:description delegate:self cancelButtonTitle:nil otherButtonTitles:#"Later", #"Yes", nil];
askToPurchase.delegate = self;
[askToPurchase show];
}
Next method is UIAlertViewDelegate method. Is called when i click YES on alertView from first method.
#pragma mark - UIAlertViewDelegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"%#", completeIdentifier);
if (alertView == askToPurchase) {
NSLog(#"[TSIAPHelper] Clicked YES. Prepare...");
if ([SKPaymentQueue canMakePayments]) {
NSLog(#"[TSIAPHelper] Prepare to purchase [%#].",completeIdentifier);
SKProductsRequest *request =[[SKProductsRequest alloc] initWithProductIdentifiers:
[NSSet setWithObject:completeIdentifier]];
request.delegate = self;
[request start];
pleaseWait = [[UIAlertView alloc] initWithTitle:#"Please wait..." message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activity startAnimating];
[pleaseWait addSubview:activity];
activity.frame = CGRectMake(125, 50, 36, 36);
[pleaseWait show];
}
else {
NSLog(#"[TSIAPHelper] Purchase [FAILURE]. Prohibited by Parentar Control or something like that.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Prohibited." message:#"Parental Control is enabled, cannot make a purchase. Turn off and try again." delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alert show];
}
}
}
The problem is: Whenever and anywhere i want to Log variable completeIdentifier i've got crash with selected line: 0x39e965d0: ldr r3, [r4, #8] but i don't know what's that mean. And this line is selected:
NSLog(#"%#", completeIdentifier);
How can i fix it? In Cocoa Touch is working perfectly. When i use cocos2d isn't.
I guess you aren't using ARC. In this case completeIdentifier will be autoreleased.
Cocos2d clears the autoreleasepool every frame, whereas in Cocoa this is not strictly defined but may still crash. You can fix this by retaining or copying the string.
completeIdentifier = [NSString stringWithFormat:#"%#.%#", [[NSBundle mainBundle] bundleIdentifier], aIdentifier];
completeIdenfitier = [completeIdentifier retain];
Hi i have searched here on the forum but no help found so i am posting it new. Here is the scenario, i am creating a mfmailcomposeviewcontroller in the main rootviewcontroller, i am displaying it by calling presentviewcontroller but when it is dismissed i get this error :
error: address doesn't contain a section that points to a section in a object file
The code i am using is given below:
-(void) mailButtonTapped
{
if ([MFMailComposeViewController canSendMail]) {
mailViewController_ = [[MFMailComposeViewController alloc] init];
mailViewController_.mailComposeDelegate = self;
[mailViewController_ setSubject:#"Try ..."];
[mailViewController_ setMessageBody:#"Hey I just tried ..." isHTML:NO];
NSData *videoData = [NSData dataWithContentsOfURL:movieURL_];
[mailViewController_ addAttachmentData:videoData mimeType:#"video/quicktime" fileName:#"Video.mov"];
[self presentViewController:mailViewController_ animated:YES completion:nil];
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sharing Not Possible" message:#"Configure your mail to send the mail" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
}
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSString *title = #"Email";
NSString *msg = nil;
if (result == MFMailComposeResultFailed)
msg = #"Unable to send, check your email settings";
else if (result == MFMailComposeResultSent)
msg = #"Email Sent Successfully!";
else if (result == MFMailComposeResultCancelled || result == MFMailComposeResultSaved)
msg = #"Sending Cancelled";
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
[self dismissViewControllerAnimated:YES completion:nil];
}
After dismissing i receive the error:
error: address doesn't contain a section that points to a section in a object file
Please help me
I also had this error but with another scenario. I had a block property defined with #property (assign, nonatomic).
To fix this issue, I declared my block property with #property (copy, nonatomic).
Cheers
This error happens when an object / pointer is being accessed that doesn't exist anymore . And can also cause other bad access, 0x00000 value accessed etc.. errors.
So you are deleting/releasing a pointer , and then accessing later .
From looking at the code, and this is just a guess without debugging, you set the second AlertView's delegate to self, but then immediately dismiss the viewcontroller.
Try dismissing after the alert view is dismissed or the button is pressed, or maybe just setting the AlertView delegate to nil.
Even if that's not exactly the error, the main reason is somewhere you are releasing an object then trying to call a function or access it.
You can use it like this:
MFMailComposeViewController *mailViewController_ = [[MFMailComposeViewController alloc] init];
mailViewController_.mailComposeDelegate = self;
[mailViewController_ setSubject:#"Try ..."];
[mailViewController_ setMessageBody:#"Hey I just tried ..." isHTML:NO];
NSData *videoData = [NSData dataWithContentsOfURL:movieURL_];
[mailViewController_ addAttachmentData:videoData mimeType:#"video/quicktime" fileName:#"Video.mov"];
[self presentViewController:mailViewController_ animated:YES completion:nil];
[mailViewController_ release];
I also had this problem but caused by a very silly error, i wrote a property called frameon a class that inherits from UIView (it was a UITableViewCell but i think this would happen with every class that inherits from UIView) this overwrote the original frame property and caused this error.
Fixed just by changing property name.
The completion expects a block to be called when the animation of dismissal is completed.
Just remove "completion:nil" and it should work!
Regards.
I've try to send an email over the UIActivityViewController and it works fine, but the mail view is not dismissed after the email was send. It also wont't dismiss when the user has pushed the Cancel button.
What can I do to fix the problem?
Here the code I have used:
//conf share view
NSString *textToShare = #"Teststring!";
UIImage *imageToShare = fetchedImage;
NSURL *urlToShare = [NSURL URLWithString:string_url];
NSArray *activityItems = [[NSArray alloc] initWithObjects:textToShare, imageToShare,urlToShare,nil];
UIActivity *activity = [[UIActivity alloc] init];
NSArray *applicationActivities = [[NSArray alloc] initWithObjects:activity, nil];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:applicationActivities];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact,
UIActivityTypePostToWeibo,
UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint];
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
NSLog(#"CompletionHandler was called!");
}];
Thanks!
I have the solution. You want to present the UIActivityViewController in your fundamental, i.e. your highest or last view controller. In my case, this worked:
[[[self parentViewController] parentViewController] presentViewController:activityViewController animated:YES completion:nil];
Try out yourself how many times you have to call parentViewController until you are accessing the highest view controller. In my simple MasterView/DetailView app, I had to call it twice.
it always come in this completion handler after the posting is done weather it was successfull or not
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if (completed)
{
UIAlertView *objalert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Posting was successful" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objalert show];
objalert = nil;
}else
{
UIAlertView *objalert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Posting was not successful" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objalert show];
objalert = nil;
}
}];
then try to dismiss it on the completitionHandler
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
//Dismiss here
}];