Get cropped photo of person in MonoTouch (on iPhone) - iphone

I'm able to get original image of person from address book but I would like to ask, if there is any way how to get cropped image (in case, user has taken a picture and zoomed just some part of it). I found a way, how to do this in objective-c:
NSData *imageData = [(NSData *)ABPersonCopyImageDataWithFormat(
recordRef, kABPersonImageFormatThumbnail) autorelease];
I can't find a way how to get this thumbnail in MonoTouch. I found just this enumeration which has definitely something to do with that, but nothing else :(
http://docs.go-mono.com/MonoTouch.AddressBook.ABPersonImageFormat/Members
Please, has anybody any idea?
Thanks in advance

Try MonoTouch.AddressBook.ABPerson.Image propery. And MonoTouch.AddressBook.ABPerson.HasImage property to shure that MonoTouch.AddressBook.ABPerson has a picture.

When you right-click on the Image property of an ABPerson instance and then select 'Go to declaration', you will find the code Monotouch implements for this property:
public NSData Image {
get {
return (NSData)Runtime.GetNSObject(ABPerson.ABPersonCopyImageData(this.Handle));
}
set {
if (!ABPerson.ABPersonSetImageData(this.Handle, (value ? value.Handle : IntPtr.Zero), &IntPtr ))
{
throw CFException.FromCFError();
}
}
}
You see that a ABPerson.ABPersonCopyImageData method is used to return the Image, so there is a method to get the image, but I think it is marked Internal.
So I think we need to Bind the Objective C functions ourself. Is there anybody out there who knows how to do this or has any experience with this?

Related

How to avoid image duplication in image gallery of iOS device?

I want to store the images to photo gallary. But if images are already available at photo gallary then how to distinguish for whether imgaes are already exists or not? I didnt find property like unique identifier or should i compare by taking data in the form of NSdata?
thanks..
You can maintain a dictionary of hashes for each the image in the photo gallery, and then show only additional images who do not hashes are not present in the dictionary
As a reminder, you can check for an object in a dictionary by doing:
if ([myDictionary objectForKey:variableStoringImageHash] == nil) {
//No such image present
}
else {
//image is present
}
For a bit about hashing an image, this might help:
iPhone: fast hash function for storing web images (url) as files (hashed filenames)
I am not sure if what eitan27 says will work so as an alternative I would say that your best hope is comparing NSData objects. But this as you can see will become very tedious as there will be n number of images in library and comparing each one for repetition does't make sense , still if you want to compare data you look at this answer which will give you a fraction of how much the data is matching.

Monotouch binding for ABPersonCopyImageDataWithFormat

In Monotouch the only way you can get the Image from an ABPerson instance is using the Image property which returns the image in its original size.
In Objective-C there is a function called ABPersonCopyImageDataWithFormat (http://developer.apple.com/library/ios/#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABPersonCopyImageDataWithFormat), which can return the thumbnailed image from the contact,
but Monotouch does not provide a binding for this.
Does any body know how to call this function in Monotouch or how to create its binding?
Thanks,
Danny
Just found the solution (by disassemling the monotouch.dll and look carefully at the internal code)
[DllImport("/System/Library/Frameworks/AddressBook.framework/AddressBook")]
private static extern IntPtr ABPersonCopyImageDataWithFormat(IntPtr handle, ABPersonImageFormat format);
ABPersonImageFormat format = ABPersonImageFormat.Thumbnail;
NSData data = new NSData(ABPersonCopyImageDataWithFormat(person.Handle, format));
UIImage imgThumb = UIImage.LoadFromData(data);
Good luck with it !!!
It's bound as MonoTouch.AddressBook.ABPerson.CopyImage (ABPersonImageFormat format);

Where is NSAlert.h in the iOS SDK?

According to iOS's Using and Creating Error Objects, one can display a error object with the following code.
NSError *theError = nil;
BOOL success = [myDoc writeToURL:[self docURL] ofType:#"html" error:&theError];
if (success == NO) {
// Maybe try to determine cause of error and recover first.
NSAlert *theAlert = [NSAlert alertWithError:theError];
[theAlert runModal]; // Ignore return value.
}
Unfortunately, I am not smart enough to figure out how to include NSAlert.h.
Any help would be appreciated.
That document is referring to the (OS X) AppKit framework. Right after that box, it says:
(UIAlertView, the UIKit class corresponding to NSAlert, has no equivalent method for alertWithError:.)
You can still use UIAlertView, but you'll need to manually populate the title, message, and button strings from the error.
NSAlert isn't availble in UIKit. the example it just an copy past of the OS X doc.
This code in Listing 2-1 uses the
returned NSError to display an error
alert to the user immediately.
(UIAlertView, the UIKit class
corresponding to NSAlert, has no
equivalent method for
alertWithError:.) Error objects in the
Cocoa domain are always localized and
ready to present to users, so they can
often be presented without further
evaluation.
Its in the AppKit framework.
NSAlert
Import that framework to your project in xcode, then use the #include directive at the top of your code
there are also some samples of using NSAlert if you go to that page and look at the "Sample Code". I always do that if I'm unsure of any part of a new framework

How to Load an array into OpenFlow

I'm trying to implement openFlow in my project but I cant seem to get the images to show up on my uiview. What isnt clear to me is once I have the dictionary of image links, how do i tell AFOpenView that I want to use that dictionary object as my data source?
I've looked at the demo code and I see that when the flickr request finishes, he saves a copy of the dictionary results, counts them, and then tells OpenFlowView that there are x number of images, but what is never clear is how he tells OpenFlowView to use the dictionary with the results?
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
// Hold onto the response dictionary.
interestingPhotosDictionary = [inResponseDictionary retain];
int numberOfImages = [[inResponseDictionary valueForKeyPath:#"photos.photo"] count];
[(AFOpenFlowView *)self.view setNumberOfImages:numberOfImages];
}
See here: http://blog.objectgraph.com/index.php/2010/04/09/how-to-add-coverflow-effect-on-your-iphone-app-openflow/
This tutorial seems to suggest that you have to call the view's setImage method multiple times, once per image.
This tells me that the implementation is confusing and weird, but for this you have to blame the component's author.
The images are loaded on demand in the 'updateCoverImage:' method of AFOpenFlowView.m
'updateCoverImage:' calls 'openFlowView:requestImageForIndex:' in AFOpenFlowViewController.m, which uses interestingPhotosDictionary.
So, it is called on demand whenever an image needs to be loaded. It wraps an operation queue so the images are loaded outside the main thread.

Objective C Comparing the name of an UIImageView

i have a question, totally newbie but i would like to know how can i get to compare and image in an if else statement?
i have an image lets say it is "img.png" and i have it displayed in an imageview already in the application, however in the codes i would like to compare if the image show is "img.png"
I understand that for string we use the isEqualToString:, so if it's for an image what would i use?
example would be :
if ([img.image isEqual:#"img.png"])
What i've typed is something like this but it isnt working. could anyone point me in the right direction? thanks alot
Much appreciated!
If you first got the image using [UIImage imageNamed:#"img.png"] you could compare the image object pointers: if (img.image == [UIImage imageNamed:#"img.png"]).
But: I'd consider this bad design and would rather use another way of identifying images, like storing the name of the image in another instance variable.