Retrieving the UIImage of a TTPhotoViewController - iphone

I have a TTPhotoViewController subclass working nicely. I need to be able to give the option of saving the image downloaded to the user's saved photos directory. I am having problems actually retrieving a UIImage object.
After looking through the API I have seen that TTPhoto (which is one of the properties of the view controller) doesn't actually contain a UIImage to use. However, within the API there is a TTImageView class which has a UIImage property. TTPhotoView derives from TTImageView and there is one of these in my view controller class called "PhotoStatusView". However when I access this I do not get a UIImage back.
Any ideas?

Check this site out add save to album functionality
Here is the code from the site:
_clickActionItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAction
//TTIMAGE(#"UIBarButtonReply.png")
target:self action:#selector(clickActionItem)];
UIBarButtonItem* playButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemPlay target:self action:#selector(playAction)] autorelease];
playButton.tag = 1;
UIBarItem* space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
_toolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0, screenFrame.size.height - TT_ROW_HEIGHT,
screenFrame.size.width, TT_ROW_HEIGHT)];
_toolbar.barStyle = self.navigationBarStyle;
_toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleTopMargin;
NSString *searchCaption = #"Photo from networked";
NSRange range = [[_centerPhoto caption] rangeOfString:searchCaption];
if (range.location == NSNotFound) {
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space,_clickActionItem, nil];
[_innerView addSubview:_toolbar];
}else{
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space, nil, nil];
[_innerView addSubview:_toolbar];
}
and this
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(6 == actionSheet.tag)
{
if(0 == buttonIndex)//save page
{
NSLog(#"photo: %#", [_centerPhoto URLForVersion:TTPhotoVersionLarge]);
NSURL *aUrl = [NSURL URLWithString:[_centerPhoto URLForVersion:TTPhotoVersionLarge]];
NSData *data = [NSData dataWithContentsOfURL:aUrl];
UIImage *img = [[UIImage alloc] initWithData:data];
NSLog(#"photo:class %#", [img class]);
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

You should try using:
UIImage *image = [[TTURLCache sharedCache] imageForURL:URL];

Related

Can't tweet an image

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
forTweet = TRUE;
switch(buttonIndex)
{
case 0:
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Set the initial tweet text. See the framework for additional properties that can be set.
NSMutableString * twitterString = [displayString mutableCopy];
[twitterString appendString: #" #LoviOtvet"];
[tweetViewController setInitialText: twitterString];
UIImage * imageAnswer = [self getImageFromURL: [self strToUrlConverter]];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
[tweetViewController addImage: [self mergeImage:imageAnswer withImage:imageLogo strength:1]];
break;
}
I added . No errors, but not working.
Facebook and saving to device working. Tweeting not. Why not?
Your problem is this line:
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
For this method, you would need to need to provide a file path, not a name. You have two choices:
UIImage * imageLogo = [UIImage imageNamed:]
or (for a file in the bundle):
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile:path];

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];

toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause

Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:#selector(playaudio:)];
systemItem1.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:#selector(pause:)];
systemItem2.style = UIBarButtonItemStyleBordered;
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, modalBarButtonItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[modalBarButtonItem release];
[flexItem release];
-(void) playaudio: (id) sender {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
Any ideas how i can do that.
Essentially, every time the play/pause status is updated, you're going to want to run a method to update the toolbar. Something like this should work.
You can create a method like this:
-(void)playPause{
if(audioPlayer == nil){
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[fileURL release];
}
UIBarButtonSystemItem buttontype = UIBarButtonSystemItemPlay;
if([audioPlayer isPlaying]){
[audioPlayer pause];
}
else {
[audioPlayer play];
buttontype = UIBarButtonSystemItemPause;
}
UIBarButtonSystemItem *item = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:buttontype
target:self
action:#selector(playPause)] autorelease];
self.toolbar.items = [NSArray arrayWithObject:item];
}
I had a much simpler solution to that issue:
- (void) setStartStopButton:(BOOL)startorstop
{
UIBarButtonItem *startStopButton = nil;
if (startorstop == YES) {
startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:#selector(startStopAction:)];
}
else
{
startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(startStopAction:)];
}
self.navigationItem.rightBarButtonItem = startStopButton;
[startStopButton release];
}
- (IBAction)startStopAction:(id)sender
{
if (task.isActive)
{
[task stopTask];
}
else
{
[task startTask];
}
[self setStartStopButton:task.isActive];
}
And then I call the first method to set the button in viewWillAppear as well to set the button before the view appears onscreen.

UIButton view unrecognized selector sent to instance

Basically i have a UIToolbar on the main window and when i hit the infobutton to open a modal view application is crashing with output message that UIButton view unrecognized selector sent to instance. Can someone please help me to solve this output message.
UIButton *infoItem = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoItem.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
infoItem.backgroundColor = [UIColor clearColor];
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
- (void) playaudio: (id) sender {
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
// Making sure the audio is at the start of the stream.
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
- (void)stop: (id) sender
{
[audioPlayer stop];
}
- (void)displayModalViewaction
{
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
}
Appreciate help to solve this.
Thanks in advance.
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
should be
[infoItem addTarget:self action:#selector(displayModalViewaction) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
because displayModalViewaction do not receive any parameters.
use #selector(displayModalViewaction) to add event;
or change method define with: -(void)displayModalViewaction:(id)sender;

iphone UIImage array memory leak

I had that question before, tried as answered, but still it does not work.
Before I allocated with:
imageArray_danceright = [[NSArray alloc] initWithObjects:
I came adviced to do like bellow, but now it crash immediatly after second anim is called:
//init areas:
imageArray_stand = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankieSingtRetime_0001" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankieSingtRetime_0002" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0003" ofType:#"jpg"]],nil];
imageArray_danceleft = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0001" ofType:#"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0040" ofType:#"jpg"]],nil];
imageArray_danceright = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0041" ofType:#"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0080" ofType:#"jpg"]],nil];
//start:
[myimageview setAnimationImages:imageArray_stand];
myimageview.animationDuration = 0.23;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0.0;
myimageview.image = [myimageview.animationImages objectAtIndex:1];
[myimageview startAnimating];
// ------ in my 'touchesBegan" i have:
if ([touch view] == overlay_fuesserechts) {
[myimageview.animationImages release];
[myimageview setAnimationImages:imageArray_danceright];
myimageview.animationDuration = 2.0;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0;
[myimageview startAnimating];
}
- (void)dealloc {
[imageArray_stand release];
imageArray_stand=nil;
[imageArray_danceright release];
imageArray_danceright=nil;
[imageArray_danceleft release];
imageArray_danceleft=nil;
super dealloc];
}
Its starts with the "stand" animation.. but when i fire touchesbegan it crashes immediatly at:
[myimageview setAnimationImages:imageArray_danceright];
and i have no idea. I tried so many things. Now I hope you have an idea.
thx
chris
Because you are autoreleasing your image arrays, by the time it gets to TouchesBegan they have already been released.
Who told you to use arrayWithObjects instead of alloc - initWithObjects ?
It sounds like your earlier version would have been more correct. What was the problem you were having with that?
You surely don't need the [myimageview.animationImages release]; at the beginning of touches began. You are creating autoreleased array objects and assigening them to the imageview which releases them.