I am working on an iPhone application which works with AddressBook contacts. I am trying to fetch the image from address book and edit then save that image.
But I don't know how to update and save that image. Please give me a solution if anybody knows about this.
Thanx in advance.
Here's the .h file
ABAddressBookRef addressBook;
IBOutlet UILabel *lblName;
IBOutlet UIImageView *imgGet;
UIImage *imgContact;
NSString* name;
The .m file
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
name = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
lblName.text = name;
if(ABPersonHasImageData(person)) {
CFDataRef imageData = ABPersonCopyImageData(person);
imgContact = [UIImage imageWithData:(NSData *)imageData];
imgGet.image = imgContact;
CFRelease(imageData);
} else {
imgGet.image = [UIImage imageNamed:#"ABPicturePerson#2x.png"];
}
[self.navigationController dismissViewControllerAnimated:(YES) completion:nil];
return NO;
}
Here After fetching image and name from above method then i open gallery and select an image then click on below method button but image not saved or update in address book
-(IBAction)btnSaveClicked {
ABRecordRef person = ABPersonCreate();
NSData *dataRef = UIImagePNGRepresentation(imgGet.image);
CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
ABPersonSetImageData(person, (CFDataRef)dataRef, nil);
CFErrorRef error;
ABPersonRemoveImageData(person, &error); // <-- clean any image first from ref
//ABAddressBookSave(addressBook, &error);
ABPersonSetImageData(person, cfdata, &error);
ABAddressBookSave(addressBook, &error);
CFRelease(cfdata);
}
This link is hint for me that how to update your image..
http://davidbits.blogspot.in/2010/01/iphone-update-addressbook-contact.html
declare 1 object in .h file
NSInteger recordID;
then open contact list and for fetching the contact we use this method
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
recordID = ABRecordGetRecordID(person);
name = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
lblName.text = name;
if(ABPersonHasImageData(person)) {
CFDataRef imageData = ABPersonCopyImageData(person);
your uiimage = [UIImage imageWithData:(NSData *)imageData];
you uiimageview.image = your uiimage;
CFRelease(imageData);
} else {
you uiimageview.image = [UIImage imageNamed:#"ABPicturePerson#2x.png"];
}
[self.navigationController dismissViewControllerAnimated:(YES) completion:nil];
//[self.navigationController dismissModalViewControllerAnimated:YES];
return NO;
}
then write action for save detail or update detail
-(IBAction)btnSaveClicked
{
CFErrorRef *aberror = NULL;
ABRecordRef aRecord = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
if (aRecord) {
NSData *dataRef = UIImagePNGRepresentation(your uiimage);
ABPersonSetImageData(aRecord, (CFDataRef)dataRef, nil);
}
ABAddressBookSave(addressBook, aberror);
//BOOL didAdd = ABAddressBookSave(addressBook, nil);
CFRelease(addressBook);
}
sure you will get result..
Use ABRecordRef person when you want to update:
Firstly create ABAddressBookRef:
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
Now check person has image:
if(ABPersonHasImageData(person))
{
//remove image as we want to update to new image
NSError *anError;
if(ABPersonRemoveImageData(person, &anError))
NSLog(#"removed");
else
NSLog(#"%#",[anError description]);
}
Update
UIImage *image = image here;
NSData *picData = UIImageJPEGRepresentation(image, 0.9f);
ABPersonSetImageData(person, (CFDataRef)picData, nil);
call saving function:
ABAddressBookSave(addressBookRef, nil);
Related
I am developing application which changes the contact picture of the contact when i select any contact from the contact picker .
Here is the code I have implemented so far,
(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
CFErrorRef error=nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"birthday.jpg" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if(ABPersonHasImageData(person))
{
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
if (ABPersonSetImageData(person,cfDataRef, &error))
{
NSLog(#"Set contact photo %#", error);
if (ABAddressBookHasUnsavedChanges(addressBook))
{
NSLog(#"Changes made to address book");
}
else {
NSLog(#"No changes made to address book");
}
if (ABAddressBookSave(addressBook, &error))
{
NSLog(#"Saved");
}
else {
NSLog(#"Not saved");
}
}
CFRelease(cfDataRef);
// TODO: release peoplePicker (and refactor code to not have it global)
[self dismissModalViewControllerAnimated:YES];
return NO;
}
But when I debug and look at ABAddressBookHasUnsavedChanges it is returning false and image is not getting set to the contact. What might be the reason?
I wrote this method in shouldContinueAfterSelectingPerson; I have checked the image and it's not null and ABPersonSetImageData is returning TRUE.
I have updated the code , checked for the image set already , if yes then i am removing that image and saving the saving the address book and relased CFDataRef.
But still its not working
Update:2
I have edited the code , please look at the changes.
I have written all my code in this method
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { } ,
So definately there will be person who is already in address book . But i tried according to above code still that ABAddressBookHasUnsavedChanges returning false
You are creating the data wrong - you should be doing it like this.
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
Also try setting a break point somewhere in the beginning and check if something isn't nil or doesn't have wrong value.
UPDATE
I have tried it in actual Xcode, just to test. The reason why your code tells you that address book has no unsaved changes is because you haven't added the record inside it yet - otherwise you are setting an image on a contact that hasn't been added to address book yet (that is not a problem, you can do that without any problems, but the contact itself is not in the address book until you save it). So the magical line of code for you will probably be this:
ABAddressBookAddRecord(addressBook, person, &error);
This is the full code I've been using.
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"sky_main" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if (ABPersonHasImageData(person)) {
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
ABAddressBookAddRecord(addressBook, person, &error);
if (ABPersonSetImageData(person, cfDataRef, &error)) {
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(#"has unsaved changes");
} else {
NSLog(#"nothing to save");
}
if (ABAddressBookSave(addressBook, &error)) {
NSLog(#"saved");
} else {
NSLog(#"not saved");
}
}
ABUnknownPersonViewController *ctr = [[ABUnknownPersonViewController alloc] init];
ctr.unknownPersonViewDelegate = nil;
ctr.displayedPerson = person;
ctr.allowsAddingToAddressBook = YES;
ctr.allowsActions = YES;
ctr.hidesBottomBarWhenPushed = YES;
// [[[CCDirector sharedDirector] view] addSubview:ctr.view];
CFRelease(person);
CFRelease(addressBook);
I am also using the AddressBookUI, but you don't have to use it (I just wanted to see the changes to the unsaved contact).
UPDATE 2
Ok, just created a new project just to try. It looks like the problem was actually with the ABAddressBookRef. You have to get that from the peoplePicker argument of the delegate method. Since it is the delegate method you don't have to add the record because it already exists. This is the code that has to work (tried it 2 minutes ago). Let me know :)
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
CFErrorRef error = nil;
ABAddressBookRef addressBook = peoplePicker.addressBook;
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"cat" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
if (ABPersonHasImageData(person)) {
ABPersonRemoveImageData(person, &error);
ABAddressBookSave(addressBook, &error);
}
if (ABPersonSetImageData(person, cfDataRef, &error)) {
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(#"has unsaved changes");
} else {
NSLog(#"nothing to save");
}
if (ABAddressBookSave(addressBook, &error)) {
NSLog(#"saved");
} else {
NSLog(#"not saved");
}
}
CFRelease(addressBook);
return YES;
}
Btw., since it is the delegate method do not release the person otherwise it will crash horribly - just kidding, it will crash, because it tries to access memory that doesn't exist.
My client wants to share an image on Instagram. I have implemeted sharing image on instagram.But i could not share it with a special hashtag. Here is my code so far.
- (IBAction)sharePhotoOnInstagram:(id)sender {
UIImagePickerController *imgpicker=[[UIImagePickerController alloc] init];
imgpicker.delegate=self;
[self storeimage];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
CGRect rect = CGRectMake(0 ,0 , 612, 612);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/15717.ig"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
dic.UTI = #"com.instagram.photo";
dic.delegate = self;
dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
dic = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
dic.delegate = self;
[dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
// [[UIApplication sharedApplication] openURL:instagramURL];
}
else
{
// NSLog(#"instagramImageShare");
UIAlertView *errorToShare = [[UIAlertView alloc] initWithTitle:#"Instagram unavailable " message:#"You need to install Instagram in your device in order to share this image" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
errorToShare.tag=3010;
[errorToShare show];
}
}
- (void) storeimage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"15717.ig"];
UIImage *NewImg = [self resizedImage:picTaken :CGRectMake(0, 0, 612, 612) ];
NSData *imageData = UIImagePNGRepresentation(NewImg);
[imageData writeToFile:savedImagePath atomically:NO];
}
-(UIImage*) resizedImage:(UIImage *)inImage: (CGRect) thumbRect
{
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
4 * thumbRect.size.width, // rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);
// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);
// Get an image from the context and a UIImage
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap); // ok if NULL
CGImageRelease(ref);
return result;
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate
{
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
interactionController.delegate = self;
return interactionController;
}
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
{
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action
{
// NSLog(#"5dsklfjkljas");
return YES;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action
{
// NSLog(#"dsfa");
return YES;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
{
// NSLog(#"fsafasd;");
}
Note : This is working fine.
I have followed their documentation on http://instagram.com/developer/iphone-hooks/ but couldn't get better idea from it!. Now don't know what to do next step for sharing an image with hashtag and other information.
Secondly I want to retrieve all the images shared with a particular hashtag into the application.
Please guide me! Thanks in advance!
First, from iPhone Hooks, under 'Document Interaction':
To include a pre-filled caption with your photo, you can set the annotation property on the document interaction request to an NSDictionary containing an NSString under the key "InstagramCaption". Note: this feature will be available on Instagram 2.1 and later.
You'll need to add something like:
dic.annotation = [NSDictionary dictionaryWithObject:#"#yourTagHere" forKey:#"InstagramCaption"];
Second, you'll need to take a look at Tag Endpoints if you want to pull down images with a specific tag.
Heyo!
Is there a way how when a user taps a button it can add or update a contact into the actual Apple Contacts Book? Some festivals have email responses include a "name card" that the receiver can download and find in their contact book.
If doing this in iOS 9 or later, you should use the Contacts framework:
#import Contacts;
You also need to update your Info.plist, adding a NSContactsUsageDescription to explain why your app requires access to contacts.
Then, when you then want to programmatically add the contact, then you can do something like:
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Access to contacts." message:#"This app requires access to contacts because ..." preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:#"Go to Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:#{} completionHandler:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:TRUE completion:nil];
return;
}
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
dispatch_async(dispatch_get_main_queue(), ^{
// user didn't grant access;
// so, again, tell user here why app needs permissions in order to do it's job;
// this is dispatched to the main queue because this request could be running on background thread
});
return;
}
// create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.familyName = #"Doe";
contact.givenName = #"John";
CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:#"312-555-1212"]];
contact.phoneNumbers = #[homePhone];
CNSaveRequest *request = [[CNSaveRequest alloc] init];
[request addContact:contact toContainerWithIdentifier:nil];
// save it
NSError *saveError;
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(#"error = %#", saveError);
}
}];
Or, even better, if you want to add the contact using the ContactUI framework (giving the user visual confirmation of the contact and let them tailor it as they see fit), you can import both frameworks:
#import Contacts;
#import ContactsUI;
And then:
CNContactStore *store = [[CNContactStore alloc] init];
// create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.familyName = #"Smith";
contact.givenName = #"Jane";
CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:#"301-555-1212"]];
contact.phoneNumbers = #[homePhone];
CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:contact];
controller.contactStore = store;
controller.delegate = self;
[self.navigationController pushViewController:controller animated:TRUE];
My original answer, using the AddressBook and AddressBookUI frameworks for iOS versions before 9, is below. But if only supporting iOS 9 and later, use the Contacts and ContactsUI frameworks as outlined above.
--
If you want to add a contact to the user's address book, you use AddressBook.Framework to create a contact, and then you use the AddressBookUI.Framework to present the user interface to allow the user to add it to their personal address book using ABUnknownPersonViewController. Thus, you can:
Add AddressBook.Framework and AddressBookUI.Framework to your list under Link Binary With Libraries;
Import the .h files:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
Write the code to create a contact, e.g.:
// create person record
ABRecordRef person = ABPersonCreate();
// set name and other string values
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef) venueName, NULL);
if (venueUrl) {
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) venueUrl, kABPersonHomePageLabel, NULL);
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
}
if (venueEmail) {
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) venueEmail, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
}
if (venuePhone) {
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSArray *venuePhoneNumbers = [venuePhone componentsSeparatedByString:#" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
CFRelease(phoneNumberMultiValue);
}
// add address
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
if (venueAddress1) {
if (venueAddress2)
addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:#"%#\n%#", venueAddress1, venueAddress2];
else
addressDictionary[(NSString *) kABPersonAddressStreetKey] = venueAddress1;
}
if (venueCity)
addressDictionary[(NSString *)kABPersonAddressCityKey] = venueCity;
if (venueState)
addressDictionary[(NSString *)kABPersonAddressStateKey] = venueState;
if (venueZip)
addressDictionary[(NSString *)kABPersonAddressZIPKey] = venueZip;
if (venueCountry)
addressDictionary[(NSString *)kABPersonAddressCountryKey] = venueCountry;
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
// let's show view controller
ABUnknownPersonViewController *controller = [[ABUnknownPersonViewController alloc] init];
controller.displayedPerson = person;
controller.allowsAddingToAddressBook = YES;
// current view must have a navigation controller
[self.navigationController pushViewController:controller animated:YES];
CFRelease(person);
See the ABUnknownPersonViewController Class Reference or the Prompting the User to Create a New Person Record from Existing Data section of the Address Book Programming Guide.
To present default contact controller
Step 1:
Add ContactUi.framework into project and import
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
Step2:
Add this code
-(void)showAddContactController{
//Pass nil to show default contact adding screen
CNContactViewController *addContactVC = [CNContactViewController viewControllerForNewContact:nil];
addContactVC.delegate=self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addContactVC];
[viewController presentViewController:navController animated:NO completion:nil];
}
Step3:
For getting call back when pressing DONE or CANCEL, Add <CNContactViewControllerDelegate>and implement the delegate method.
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
//You will get the callback here
}
#import Contacts;
-(void)addToContactList
{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:#"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can add the desired contact" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:TRUE completion:nil];
return;
}
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
dispatch_async(dispatch_get_main_queue(), ^{
// user didn't grant access;
// so, again, tell user here why app needs permissions in order to do it's job;
// this is dispatched to the main queue because this request could be running on background thread
});
return;
}
// create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.givenName = #"Test";
contact.familyName = #"User";
CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:#"91012-555-1212"]];
contact.phoneNumbers = #[homePhone];
CNSaveRequest *request = [[CNSaveRequest alloc] init];
[request addContact:contact toContainerWithIdentifier:nil];
// save it
NSError *saveError;
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(#"error = %#", saveError);
}
}];
}
I try using my app ALAssetRepresentation.and when i loop om an images there are couple of image that crash the app
for(ALAsset *asset in _assets) {
NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:#"UIImagePickerControllerMediaType"];
ALAssetRepresentation *representation = [asset defaultRepresentation];
if (!representation) {
[workingDictionary release];
continue;
}
CGImageRef imageRef = [representation fullResolutionImage];//here the app crash
UIImage *img = [UIImage imageWithCGImage:imageRef];
if (!img) {
[workingDictionary release];
continue;
}
if (!img) {
[workingDictionary release];
continue;
}
[workingDictionary setObject:img forKey:#"UIImagePickerControllerOriginalImage"];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyOrientation] forKey:#"orientation"];
[returnArray addObject:workingDictionary];
[workingDictionary release];
}
in this line i get crash without any msg:
CGImageRef imageRef = [representation fullResolutionImage];
This is the crash msg
Program received signal: “0”.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
That is most likely due to running out of memory, how big are the images that cause the crash?
I had a similar problem and after hours of lookin for solution I found this - the best solution of too big Asset bug:
// For details, see http://mindsea.com/2012/12/18/downscaling-huge-alassets-without-fear-of-sigkill
#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
// Helper methods for thumbnailForAsset:maxPixelSize:
static size_t getAssetBytesCallback(void *info, void *buffer, off_t position, size_t count) {
ALAssetRepresentation *rep = (__bridge id)info;
NSError *error = nil;
size_t countRead = [rep getBytes:(uint8_t *)buffer fromOffset:position length:count error:&error];
if (countRead == 0 && error) {
// We have no way of passing this info back to the caller, so we log it, at least.
NSLog(#"thumbnailForAsset:maxPixelSize: got an error reading an asset: %#", error);
}
return countRead;
}
static void releaseAssetCallback(void *info) {
// The info here is an ALAssetRepresentation which we CFRetain in thumbnailForAsset:maxPixelSize:.
// This release balances that retain.
CFRelease(info);
}
// Returns a UIImage for the given asset, with size length at most the passed size.
// The resulting UIImage will be already rotated to UIImageOrientationUp, so its CGImageRef
// can be used directly without additional rotation handling.
// This is done synchronously, so you should call this method on a background queue/thread.
- (UIImage *)thumbnailForAsset:(ALAsset *)asset maxPixelSize:(NSUInteger)size {
NSParameterAssert(asset != nil);
NSParameterAssert(size > 0);
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGDataProviderDirectCallbacks callbacks = {
.version = 0,
.getBytePointer = NULL,
.releaseBytePointer = NULL,
.getBytesAtPosition = getAssetBytesCallback,
.releaseInfo = releaseAssetCallback,
};
CGDataProviderRef provider = CGDataProviderCreateDirect((void *)CFBridgingRetain(rep), [rep size], &callbacks);
CGImageSourceRef source = CGImageSourceCreateWithDataProvider(provider, NULL);
CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef) #{
(NSString *)kCGImageSourceCreateThumbnailFromImageAlways : #YES,
(NSString *)kCGImageSourceThumbnailMaxPixelSize : [NSNumber numberWithInt:size],
(NSString *)kCGImageSourceCreateThumbnailWithTransform : #YES,
});
CFRelease(source);
CFRelease(provider);
if (!imageRef) {
return nil;
}
UIImage *toReturn = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
return toReturn;
}
This question is related to Iphone SDK, NSData and UIImage.
I am trying to create an image from the Avatar Data returned from the xmpp like the following:
<presence from='yyy#184.73.164.51/spark' to='ken#184.73.164.51/424978324712783686768453' id='Oj02v-45'><status>Away due to idle.</status><priority>0</priority><show>away</show><x xmlns='vcard-temp:x:update'><photo>a3f549fa9705e7ead2905de0b6a804227ecdd404</photo></x><x xmlns='jabber:x:avatar'><hash>a3f549fa9705e7ead2905de0b6a804227ecdd404</hash></x></presence>
So in this case, I assume that a3f549fa9705e7ead2905de0b6a804227ecdd404 is the photo data.
So How can I transfer this into NSData?
I think if I can get the NSData object,
I can easily create the UIImage, right?
I think "a3f549fa9705e7ead2905de0b6a804227ecdd404" is the photo data
this is my codes:
NSString* command = #"a3f549fa9705e7ead2905de0b6a804227ecdd404";
command = [command stringByReplacingOccurrencesOfString:#" " withString:#""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [command length]/2; i++) {
byte_chars[0] = [command characterAtIndex:i*2];
byte_chars[1] = [command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
UIImage *image = [UIImage imageWithData: commandToSend];
However,
it doesn't work.
Anyone knows what's wrong with it?
In XMPPPresence.m add this method
-(NSString *)photo {
NSXMLElement *xElement = [self elementForName:#"x" xmlns:#"vcard-temp:x:update"];
NSString *photoHash = [[xElement elementForName:#"photo"]stringValue];
return photoHash;
}
// In XMPPStream's delegate:
- (void)xmppStream:(XMPPStream *)stream didReceivePresence:
(XMPPPresence *)presence {
NSString *photoHash = [presence photo];
if ([photoHash length] > 0) { // in case when there's no photo hash
XMPPJID *rosterJID = [presence from];
BOOL requestPhoto = ... // determine if you need to request new
photo or nor
if (requestPhoto) {
NSXMLElement *iqAvatar = [NSXMLElement elementWithName:#"iq"];
NSXMLElement *queryAvatar = [NSXMLElement elementWithName:#"vCard"
xmlns:#"vcard-temp"];
[iqAvatar addAttributeWithName:#"type" stringValue:#"get"];
[iqAvatar addAttributeWithName:#"to" stringValue:[rosterJID full]];
[iqAvatar addChild:queryAvatar];
XMPPIQ *avatarRequestIQ = [XMPPIQ iqFromElement:iqAvatar];
[stream sendElement:avatarRequestIQ];
}
}
}
// And when buddy will send photo, it will be in vcard BASE64-encoded.
// You will receive it as IQ:
- (BOOL)xmppStream:(XMPPStream *)stream didReceiveIQ:(XMPPIQ *)iq {
XMPPElement *vCardPhotoElement = (XMPPElement *)[[iq
elementForName:#"vCard"] elementForName:#"PHOTO"];
if (vCardPhotoElement != nil) {
// avatar data
NSString *base64DataString = [[vCardPhotoElement
elementForName:#"BINVAL"] stringValue];
NSData *imageData = [NSData
dataFromBase64String:base64DataString]; // you need to get NSData
BASE64 category
UIImage *avatarImage = [UIImage imageWithData:imageData];
XMPPJID *senderJID = [iq from];
[self xmppStream:stream didReceiveImage:avatarImage
forBuddy:senderJID]; // this is my custom delegate method where I
save new avatar to cache
}
return NO;
}
Hope this will help you.
That is the picture hash you now have to send a vcard request which will contain the same hash for verification and binval containing the picture data in base64