I have a table which is populated by a title and image entry. This is done by the following methods:
tablePhotoViewController.m
- (IBAction)takePicture:(UIBarButtonItem *)sender {
// check #1 - make sure our source type is supported
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
// check #2 see if media type includes images
if ([mediaTypes containsObject:(NSString *)kUTTypeImage]) {
// create our image picker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// do something with this image
UIImage *imagefromcamerabutton = [info objectForKey:UIImagePickerControllerEditedImage];
// handle the case where editting was not allowed...
if (!imagefromcamerabutton) imagefromcamerabutton = [info objectForKey:UIImagePickerControllerOriginalImage];
// save to photo albumn
ALAssetsLibrary *al = [Utils defaultAssetsLibrary];
[al writeImageToSavedPhotosAlbum:[imagefromcamerabutton CGImage] metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error)
{
// once we know it's saved, grab the ALAsset and store
// it in our collection for display later
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
[self.photos addObject:myasset];
[self.tableView reloadData];
};
ALAssetsLibrary *assetslibrary = [Utils defaultAssetsLibrary];
[assetslibrary assetForURL:assetURL
resultBlock:resultblock
failureBlock:nil];
}];
[self dismissImagePicker];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.photos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
ALAsset *asset = [self.photos objectAtIndex:indexPath.row];
[cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
[cell.textLabel setText:[NSString stringWithFormat:#"Thing %d", indexPath.row+1]];
return cell;
}
This works well, but the problem is that after the user closes the app, the cell data is erased. I would like these to be able to stay as photos which are continually associated with the table unless the user taps a button to delete it from the array. From my limited understanding of how this could be done it looks like i need to somehow implement NSUserDefaults is that right or is there a better practice to achieve this goal?
NSUserDefaults is loaded completely into memory when it is accessed. This means if you data source contains more than a few items this would be really slow or it could even crash when the phone runs out of memory.
But, when I understand you code correctly you are storing the photos in the camera roll. Therefore you have some kind of URL to the image already (the assetURL in your code).
For a quick and kind of dirty solution (which I find OK for small amounts of data which does not change often, I use NSCoding. So you can add the title and the assetURL into a NSDictionary and add the dictionary to the array of your data source. Then in dealloc call
[NSKeyedArchiver archiveRootObject:self.myDataSourceArray toFile:[self pathToFileInDocumentsDirectory]];
In viewDidLoad you can then get the data back you call:
self.myDataSourceArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathToFileInDocumentsDirectory]];
If the data contains many items or the items are about to change much I tend to use Core Data or Sqlite. How to use those doesn't fit into this answer.
Related
I am developing a chat, in chat I need to share pictures, my app has a button to select an image from gallery.
This is my button code:
[upBtn2 addTarget:self action:#selector(uploadImage:) forControlEvents:UIControlEventTouchUpInside];
In gallery the user can select an image to share in chat with someone else, the conversation is just 1 to 1 chat.
This is my code.
- (IBAction)uploadImage:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
newMedia = NO;
}
}
-(void)imagePickerControllerDidCancel:
(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
Uploadedimage.image=image;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
// [self performSelector:#selector(uploadToServer) withObject:nil afterDelay:0.0];
XMPPMessage *message = [[XMPPMessage alloc] initWithType:XMPP_MESSAGE_TYPE_CHAT to:[self.contact jid]];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageStr = [GTMBase64 stringByEncodingData:imageData];
//decoding
NSData *imageData2 = [GTMBase64 decodeString:imageStr];
[message addBody:imageStr];
[[[AppDelegate appDelegate] xmppStream] sendElement:message];
XMPPJID *myJID = [[[AppDelegate appDelegate] xmppStream] myJID];
[self addMessage:#{
XMPP_TIMESTAMP: [NSDate date],
XMPP_MESSAGE_USERNAME: [myJID bare],
XMPP_MESSAGE_TEXT: imageStr,
}];
[self.tableView reloadData];
[self scrollToBottom:true];
}
- (void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Archivo guardado"
message: #"Error al guardar archivo"\
delegate: nil
cancelButtonTitle:#"Aceptar"
otherButtonTitles:nil];
[alert show];
}
}
I am getting an issue, the code it is showing
2014-10-14 11:01:21.973 Ucity[2907:60b] messages: (
{
text = "/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAE9KADAAQAAAABAAAFcAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/..."
I need to see the image there but for some reason its showing that text.
I am using GTMBase 64 library
#import "GTMBase64.h"
Can someone help me to fix this problem?
That cryptic text is the image. It's just the base-64 string representation of the image. I assume you're doing this base-64 encoding for a reason (e.g. this format is required by some service to which you are uploading the image).
I'm presuming from your question that you want to show the image somewhere. I'm just trying to reconcile this with this code, in which you retrieved the image, converted it to a base-64 string, then discarded the original image, and now you're asking us why you're only seeing the string.
If you need the image again, there are a couple of options:
Keep the UIImage (or the NSData) that you grabbed in didFinishPickingMediaWithInfo.
You could alternatively convert the base-64 string back to a NSData (and then create a UIImage from that). This is a pretty convoluted approach, but it would work.
As an aside, if you wanted, you could probably replace GTMBase64.h with the native base-64 methods that Apple exposed in iOS 7. See https://stackoverflow.com/a/19794564/1271826.
Also, I don't personally like grabbing the UIImage and doing the PNG conversion to get the NSData. I always grab the original asset as shown here. This ensures there's no loss of information and that the resulting NSData isn't larger than it needs to be.
I started to create a iOS App, and I started to have some troubles.
I have this code:
Taking a picture:
- (IBAction)tirarFoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
Saving the picture to iOS Photos App:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self.library saveImage:image toAlbum:#"Relembre" withCompletionBlock:^(NSError *error)
{
if (error!=nil)
{
NSLog(#"Big error: %#", [error description]);
}
}];
_fotoCamera.image = image;
[picker dismissModalViewControllerAnimated:NO];
}
Ok, so far, so good!
My problem is.
In another viewController I will need to load this picture.
I Have a Core Data in my project.
Can I Save the path(or something that can I load the picture) from the picture I have saved in Photos and save into Core Data, and after load in another view??
I Just need to now the path from picture that I took.
saveImage:toAlbum:withCompletionBlock: uses writeImageToSavedPhotosAlbum:orientation:completionBlock: method, whose block contains assetURL which is exactly what you need.
I am trying to simply enable picking multiple images from photolibrary using the UIImagePickerController.I'm relatively new to XCode and I don't understand how to allow the user to pick multiple images from the UIImagePickerControler. This is my current code.Please help any body how to pick multiple images from UIImagePickerController.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex)
{
case 0:
[self takeNewPhotoFromCamera];
break;
case 1:
[self choosePhotoFromExistingImages];
default:
break;
}
}
- (void)takeNewPhotoFromCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}
-(void)choosePhotoFromExistingImages
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
}
With UIImagePickerController you are able to get only one picture. If you need to pick more you need a custom image picker, such as ELCImagePickerController. It works well! You can download it here.
As all said above, it is not possible using just ImagePickerController. You need to do it custom. Apple recently introduced PHASSET Library which makes this easy. There is a sample code also in the developer library. I am laying down the steps here.
Setup your own collection view
Load the collection view with pictures from gallery (using PHAsset, explained below)
Show each of the picture in your cellForItemAtIndexPath (using PHAsset, explained below)
In your didSelectItemAtIndexPath, keep track of which pictures were selected and add a tick mark image. Add it to a local array
When done, read from the picture array and process
Snippet code for Loading Images from gallery.
// Create a PHFetchResult object for each section in the table view.
#property (strong, nonatomic) PHFetchResult *allPhotos;
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:NO]];
if ( _isVideo == YES){
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions];
}
else {
//_allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
}
You now get all the images in your _allPhotos array which you will use like below in the cellForItemAtIndexPath
PHAsset *asset = self.allPhotos[indexPath.item];
//cell.representedAssetIdentifier = asset.localIdentifier;
cell.selectedTick.hidden = YES;
cell.isSelected = NO;
// Request an image for the asset from the PHCachingImageManager.
[self.imageManager requestImageForAsset:asset
targetSize:CGSizeMake(100, 100)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
cell.photo.image = result;
}];
return cell;
Thatz it. Hope this helps.
The main reason for using hacks like shifting the UIImagePickerController up and showing selected images underneath was because the asset library alternative would involve the user being asked for location access due to the information about where the photo was taken being available in the image metadata.
In iOS 6 the user is asked if they want to allow the app to access their photos (not location) and you get asked this question for both the asset library approach and the UIImagePickerController approach.
As such I think that hacks like the above are nearing the end of their usefulness. Here is a link to a library providing selection of multiple images using the Assets library there are others.
Happy Coding!!!
In my application i need to take video from simulator and i need to upload to server.can any one help to me.Please provide any solution as soon as possible.
Thanks & Regards
T.swathi
First, create a UIImagePickerController
On your class, you will need to implement the UIImagePickerControllerDelegate protocol and wire it up to the delegate property on the picker. You should be able to get the location of the media selected from didFinishPickingMediaWithInfo once the user has selected something.
//Check in iOS Device not in Simulator
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// this is the file system url of the media
NSURL* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
// TODO: read in data at videoUrl and write upload it to your server
}
Use this function may be help full
-(void)selectVideoButtonClicked:(id)sender{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:nil];
}
and take delege of "UIImagePickerControllerDelegate" & and import framework and using delegate function pick video from library
MobileCoreServices.framework first import in project and after than import in file CoreServices/CoreServices.h
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey: UIImagePickerControllerMediaURL] path];
NSLog(#" Video Path %#",moviePath);
NSString *fileName = [[NSString alloc]init];
//If you wants get File Name Then you can use this
if ([moviePath rangeOfString:#"//"].location != NSNotFound) {
NSString *separatorString = #"//";
NSArray *disSplit = [moviePath componentsSeparatedByString:separatorString];
fileName = disSplit[1] ;
// [self videoUploadingDirect:moviePath];
}
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Thanks :)
I know how to save a picture and this is the code until now. This code shows that the user taps a button and is able to take a photo.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil];
}
- (IBAction)ChooseExisting {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker2 animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:image];
//Save Your Image ======
UIImage *yourImage = imageview.image;//imageView.image;
NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [docDirPath stringByAppendingPathComponent:#"myImageFile.png"];
[UIImagePNGRepresentation(yourImage) writeToFile:filePath atomically:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
However I want to sort my saved pictures into 6 Category or Genres which the user has to tap using the segment view control. Is this possible to do? I am very new in ios programming and objective-c in xcode and confused with many view controllers however I really like this to work and I a cannot find any resource about this topic. Thanks.
Here is a good demo for saving images to particular album using ALAssetsLibrary:
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
I hope it will help you out...
or
You can create different libraries using this piece of code:
-(void)createDirectory:(NSString *)directoryName atFilePath:(NSString *)filePath
{
NSString *filePathAndDirectory = [filePath stringByAppendingPathComponent:directoryName];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(#"Create directory error: %#", error);
}
}
And fetch the images through their respective paths!!!
For more information see this: http://kmithi.blogspot.in/2012/08/ios-application-directory-structure.html