PDA

View Full Version : Image name retrival from webpage - Objective C


mlramya
2005-12-19, 00:51
Hi..

In Mac when an image is dragged from a web page and dropped anywhere (say desktop), it comes with name(say logo.gif) which is give in HTML file of the webpage.
i.e we get the image along with name(logo.gif) which is present in HTML file.
[Just try dragging and dropping an image from a webpage onto ur desktop]


How to retrieve the image name(logo.gif) from HTML file thru programming in Objective C.

Plz..... Reply Soon......

mlramya
2005-12-26, 05:54
Hi,

When we drag an image , image along with filename is stored in pasteboard.
When we drag an image from web-page the same thing happens.

When image is dragged from web-page:
1.To retrieve image from pasteboard:
NSData * data = [pasteBoard dataForType:NSTIFFPBoard];
NSImage *image = [[NSImage alloc] initWithData:data];

2.To retrieve file name from pasteboard:
NSArray *urlArray = [pasteBoard propertyListForType: NSURLPboardType];
NSString *imageNameTemp = [[urlArray objectAtIndex: 0] lastPathComponent];

Both are working fine for an image dragged from a web-page.

But, if I drag a image-link (image-link = when image is clicked u move to other page),
Iam getting the image correctly.

But the above code for file name retrival is not working.
It is returning the link(address of HTML page)....
EX. http://www.google.com/images/image1.html , instead of image file.

How to get the image file name correctly?

Thanks in advance.
-Ramya

drewprops
2005-12-26, 13:05
I do not understand the question, we need more (longer) explanation of what you are trying to do. Are you "scraping" the information from another website automatically or does the user drag and drop the images onto your program?

mlramya
2005-12-26, 23:44
The user drags the image from a web-page and drop it into my application.

When the dragging happens :
1.Image being dragged and
2.File name of that image (which is present in HTML file of that web-page)

both are automatically stored in pastebored.
Iam just getting the image and file-name from pasteboard , using the following code

1.To retrieve image from pasteboard:
NSData * data = [pasteBoard dataForType:NSTIFFPBoard];
NSImage *image = [[NSImage alloc] initWithData:data];

2.To retrieve file name from pasteboard:
NSArray *urlArray = [pasteBoard propertyListForType: NSURLPboardType];
NSString *imageName = [[urlArray objectAtIndex: 0] lastPathComponent];
[The above string "imageName" now holds the file-name(say logo.gif) which is present in <img> tag of HTML file ].

My point is , When user drags an image-link,
(image-link = when image is clicked u move to other page)
1. Iam getting the image correctly by using above code.
2.But, above code for file-name retrival from pasteboard is not working.
[i.e its returning me the link address which is present in <a> tag of HTMTL file.]

I want the file-name of that image.
Is there any other way to get the file name of the dragged image?

I hope , this time its clear.
Plz, ask me if i have'nt made it clear.

AsLan^
2005-12-27, 00:00
It sounds like you need to evaluate "data" to make sure its an image before using it to init an NSImage.

If it's a link, you would probably call some kind of wget function to follow the link and grab the image.

mlramya
2005-12-27, 09:56
Sorry, I could'nt get you.
What is this wget function?What does this function do?
Can u plz give me an example regarding this wget function.

It sounds like you need to evaluate "data" to make sure its an image before using it to init an NSImage.

If it's a link, you would probably call some kind of wget function to follow the link and grab the image.

Enki
2005-12-27, 13:05
Google rules (http://www.gnu.org/software/wget/wget.html). That was just the first hit.

AsLan^
2005-12-27, 16:56
Although wget is a fine utility I wasn't suggesting you actually use it, just build a function like it.

Not that using something that's already made is a bad idea... there is a utility on macs called "curl" and its functionality is very similar to wget.

mlramya
2005-12-27, 23:22
Hi,
Thanks for ur reply..

thuh Freak
2006-01-13, 11:39
i dont think wget is what hes looking for. hes saying that normally when u drag an image from www to his app, it comes with the image and the filename. but when the image was also a link, the link comes across instead of the filename.

it looks like [pasteBoard propertyListForType: NSURLPboardType] gives you an array. idk, maybe when its an image-link, the array has more than one item? (i'm just guessing) maybe the image's name will always be the last in that list?

mlramya
2006-01-21, 06:52
i dont think wget is what hes looking for. hes saying that normally when u drag an image from www to his app, it comes with the image and the filename. but when the image was also a link, the link comes across instead of the filename.

it looks like [pasteBoard propertyListForType: NSURLPboardType] gives you an array. idk, maybe when its an image-link, the array has more than one item? (i'm just guessing) maybe the image's name will always be the last in that list?

Hi,
Atleast, one person understood my problem. That's exactly what i wanted.
When I say this [pasteBoard propertyListForType: NSURLPboardType] it returns me an array.

But, the array doesn't have the file name .
The array has only 1 item, but its not image-file-name.

Array has the HTML file name
(i.e when the link is clicked, it moves to a New html page. The array is holding the new Html page address).

How to retrieve the image-file-name?

Enki
2006-01-21, 13:41
OK, now color me confused as to what the problem is.

The image filename is part of the HTML file name, specifically it is the portion following the last path separator in the HTML pathname. You should just need to filter this off and handle putting the file where you want it in the format you want it.

mlramya
2006-01-22, 06:17
The image filename is part of the HTML file name

The image fileName is'nt a part of HTML file name.
EX.
In array, the HTML file name is like:
http://Sample.com/Test.html
But, I need the link-Image, which has the name say: MyImage.jpg

AsLan^
2006-01-22, 09:02
Shouldn't you be evaluating [pasteboard types] or [pasteboard availableTypeFromArray] before invoking [pasteboard propertyListForType].

The data type you are looking for might be being stored in another type than NSTIFFPBoardType or NSURLPboardType.

Enki
2006-01-22, 14:26
The image fileName is'nt a part of HTML file name.
EX.
In array, the HTML file name is like:
http://Sample.com/Test.html
But, I need the link-Image, which has the name say: MyImage.jpg

I don't know what you are trying to make so hard. The link to the image is the name, the part after the last path separator is the name of the image on the server, everything else is how to navigate there.

Grab the :) and drag it to the desktop. Now compare that filename to the last part of the HTML name by <right-click>"copy image address" and paste that into Textedit. Then <right-click>"edit link". You will see the name is the same one as the file on the desktop, it was just parsed from the HTML URL. Make sure you are looking for the right ting as Aslan^ said and don't try to make it too difficult.

start here (http://developer.apple.com/documentation/Cocoa/Conceptual/DragandDrop/Tasks/DraggingFiles.html).