How do i get the image name of a image in imageView? - iphone

Hi i want to upload the image to HTTP server, image upload is working fine but every time the image in the server is replaced with the new image. Can some one help me to implement the commented lines below.
Thanks...
- (IBAction)upload{
//NSString *imagename=imageview.image;
//[self uploadImage:UIImageJPEGRepresentation(imageview.image, 1.0) filename:imagename];
[self uploadImage:UIImageJPEGRepresentation(imageview.image, 1.0) filename:#"facebook.jpg"];
}

To get the image's filename, capture the name during the file picking (but remember, maybe the image in the image view comes directly from the camera).
If the file upload should never overwrite a file already on the server, consider one of the following:
a) generate a unique filename for each upload (preferably in the server code, but if you also control the client, you can do it there too). You could use UUIDs as unique filenames.
OR
b) store the images in a database instead of the filesystem

Related

Ionic Capacitor Camera Delete Image

Im using the capacitor camera api to get Images. Im just interested in the base64 encoded image data. So I don't need any image pathes. Im using the following code:
const image = await Camera.getPhoto({
quality: 90,
allowEditing: false,
resultType: CameraResultType.Base64
});
I noticed the local "user data" increases by each image the user makes (tested on Android). The image gets stored somewhere (on Android its: "Android/data/com.mypackage/files/Pictures") I can't test it on iOS at the moment. I guess it behaves differently there.
Is there any good way to delete those image files?
I could get the image path if I change the resultType, read the image with the file api and convert it manually into base64 but it makes the resultType setting useless.
Any ideas?
Use Filesystem API to delete image temp when you're done in the image.
If you're using ionic native camera check here https://ionicframework.com/docs/v3/native/camera/, it has a function called cleanup() - Remove intermediate image files that are kept in temporary storage after calling camera.getPicture. Applies only when the value of Camera.sourceType equals Camera.PictureSourceType.CAMERA and the Camera.destinationType equals Camera.DestinationType.FILE_URI.

jpg images are shown in iphone simulator but not in iPhone test device

i'm sorry for my english.
i'm new in iphone development and happens to me a strange things.
I have a set of jpg images to show in a table view. When i test the app in iphone simulator everything is ok and work properly but when built and run the same code in iphone test device the same images aren't displayed.
Another strange behavior is that with a set of png images instead of jpg are shown perfectly in simulator like as in iphone test device.
Anyone can suggest me a solution?
I detect the name of image to load from a json file. This is the code that i use to show the image:
UIImageView *immaginePiadina = [[UIImageView alloc] initWithFrame:immagineValueRect];
[immaginePiadina setImage:[UIImage imageNamed:[item objectForKey:#"immagine"]]];
where [item objectForKey:#"immagine"] is an element of my json file like this:
"nome": "66",
"immagine": "pianetapiadabufalavesuviani",
"prezzo": "€ 7,50",
"nomeingrediente": [
"bufala",
"vesuviani"
]
How you can see i refer to the image with only the name of the file and without the file extension. I did it in this way to show image retina, it's wrong?
I exclude that i wrote a different case sensitive name because the png set works properly.
thanks a lot!!
There are possibly two separate issues that need separation, here is how to solve your problem.
First write a method using the NSFileManager, that given a file path verifies a file exists and has a size greater than 0. Insert a call to this everywhere you fail to open an image. If you use "imageNamed" then get the path to the bundle and create the path. If this method fails to find an image, fix it.
Second, the image decoding is different for the simulator and the device - the simulator uses the full OSX libraries. So take one image that fails to open and move it somewhere. Open it in Preview and export it using the same name but with png+alpha format. Change your code to expect a png not a jpg, and retest. Make sure you still use the first method to insure the file is there.
Once you get one success you can try other options, like using Preview exported jpegs. The reason this should work is that all of these image formats permit a huge range of options all of which iOS does not support.
Given that the current format us the problem, you can script changes using the "sips" program, which is what Preview uses.
I solve my problem using the following code:
NSString* path = [[NSBundle mainBundle] pathForResource:[item objectForKey:#"immagine"] ofType:#"jpg"];
NSLog(#"%#",path);
UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:path];
I don't know the reason but in this way i haven't any problem to display jpg in my iPhone test device. Maybe because NSBundle specify also the extension of the file.
Thanks #David-h that directed me in the right way to solve my problem.
Check the extensions of your images. If you write .PNG, in the simulator is Ok, but not in the device.

ASIHTTPRequest - Uploading files from the camera roll

I currently need to upload large files from an iDevice to a server API. For this I'm attempting to use the ASIHTTPRequest library as it automatically supports uploading large files in queued chunks (before this I simply created an NSData instance with the bytes for the entire file at once and attached this to the POST message, but this causes the application to crash on larger files due to an excessive amount of RAM usage).
The problem is that when you need to upload files and add a file to the HTTP Post message, this is the syntax you need to use:
[theUploadRequest setFile:#"" forKey:#"videoupload"];
setFile requires a file path in a string format. The problem I'm currently having is that it does not seem like you are allowed to simply take the file path from a file which is not in your applications sandbox? Since I need to upload a file which is not in my application, but outside of it in the standard cameraroll.
I tried to make this quick test to see if I could create an NSData object and fill it with data from a file in the cameraroll, providing a path to it like this:
NSData *testData = [NSData dataWithContentsOfURL:theContent.defaultRepresentation.url];
NSLog(#"THE SIZE OF THE TEST DATA: %i", testData.length);
Note that "theContent" is an instance of an ALAsset, and is a file retrieved from the cameraroll. The result of this is simply a length of 0, which I suppose means you can't simply do that.
Is there any way around this? Or would I have to somehow import the video file into the application's sandbox?
So if I understand correctly, you want to upload stuff straight from the camera roll? Based on the docs I'd say the important piece of code you need is:
NSString *filePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
The info dictionary is passed to your media picker's delegate:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info;
You should then be able to use that filePath in your call to setFile.
Take a look here probably you'll find something useful.
Hope this helps.
EDIT:
For something simpler you can use the bit of code posted in this answer
Implying that you need to copy the file somewhere before uploading it, especially if it is a big one.

Save iPhone Photo Library Path to a Plist

I want to save an image path string for a particular photo from the main iphone photo library in a plist file.
So that later I can call the plist image path string in a web view and see the photo.
I know I need the absolute path which is probably a similar path to the documents directory but:
1.) How do I get the photo name of the particular photo?
2.) Do I need to append the absolute path string to the photo name and then write to the plist?
3.) Will the path change for every install of my app - which would make a hard coded absolute path impossible to use?
Any pros want to tackle this conundrum?
If you are building an app targeting lower than iOS4.0, then you may use UIImagePickerController Class. This will let user pick one image from the phone album, which then, you can save in your app's documents directory and save the corresponding link in the info.plist file. The documents directory gets deleted when your app gets uninstalled, and so you lose the path as well.
A better way, with a disadvantage of restricting this feature (and so your app if its a main feature) to only iOS4.0 and above, would be using ALAssetsLibrary. Each photo in the iphone is represented by a unique URL that you can save, and you can refer to a particular image using that url. There is a nice tutorial blog that can help you get started with it.
Now, in your question, you are talking about a particular photo. You can not just know of any particular photo in the library, unless you let the user pick one. This brings me to answer your last question. The URL is constant for that particular image (till the iphone is reset), but unless you have any way of keeping that information between every install of your app (what exactly do mean here?), I don't know how being a unique URL is going to help you here.
EDIT:
Just saw the tag of sdk 4.0 there, please ignore the first case (the <4.0 part).
OK I think I want to use the UIImagePickerController!
Here is my code:
- (void)viewDidLoad {
self.iconPicker = [[UIImagePickerController alloc] init];
self.iconPicker.allowsEditing = YES;
self.iconPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.iconPicker.delegate = self;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
iconImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (IBAction)save {
[plist.data setValue:iconImage.image forKey:#"Icon"];
[plist.data writeToFile:plist.file atomically:YES];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
How do I save the image name of the edited image in plist string.
I know I don't want to save the CGImage data or whatever because of the load size.
I think I can pass an xcode variable into HTML through putting all my HTML into string objects and then adding variables to the objects where I need includes...
So I can create a path to the photo directory but I need to know how to get the name of the saved edited image from UIImagePickerController???

iPhone application cannot load images from an album

I have developed a small iPhone application in which a user can choose images from an image picker and that image will appear in a View. When the user first selects an image I have saved the image name to a database and I save that image to a resource folder with that name. When the main view appears I search for images from the resource folder and display that image in the main view.
When I run this application in the iPhone Simulator it successfully runs, but when I actually tried it for testing purposes on my iPhone I am not able to display images in the main view. Anyone have an idea what's wrong with that?
Hi shivang,
I think you are saving the image to NSDocumentDirectory on a first click and trying to reload that.
while retriving the files from the NSDocumentDirectory you better
first check it for the file type like jpg,png,mp3 then give the
exact name to retrive that.
NSDocumentDirectory creates unique memory for every app and its
limited,check for the size of the images u r saving.
better try this code
for (NSString* fileName in [fileManager contentsOfDirectoryAtPath:myfilepath error:nil])
{
if ( [fileName rangeOfString:#".jpg"].location != NSNotFound )
{
// Here fileName is used as a temporary variable to retrive image name
// myfilepath is the path where u stored the images
}
}