Monotouch binding for ABPersonCopyImageDataWithFormat - iphone

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);

Related

UIImage unrecognized selector sent to instance

I have the 'classic' error:
unrecognized selector sent to instance.
If I read through other comparable questions it should be memory related, however I can't find out what I'm doing wrong.
Here is what I try to do:
crop an image
give the image rounded corners
I use the brilliant code of Trevor
This is my code:
The roundedCornerImage:borderSize: is giving the trouble
NSInteger my_borderSize = 0.1;
UIImage *Image_large = [image_sel croppedImage:CGRectMake((my_width/2) -(my_height*0.66)/2, 0, my_height*0.66, my_height) ];
UIImage *roundedCornerImage_temp = [Image_large roundedCornerImage:0.8 borderSize:my_borderSize];
That method is not in Apple's UIImage class. They are extensions to UIImage written by the guy whose code you link to.
You need to make sure to add UIImage+RoundedCorner.h and UIImage+RoundedCorner.m to your Xcode project, and then in the class where you want to use roundedCornerImage:borderSize:, you should
#import "UIImage+RoundedCorner.h"
Also, I notice that you're passing in decimal values for both cornerSize and borderSize. Those are supposed to be NSInteger values, per Trevor's API. I would guess that those are in display points, but apparently, he limited it to integer values.
Update: also make sure that the UIImage+RoundedCorner.m file is listed among your Compile Sources:

Get cropped photo of person in MonoTouch (on 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?

iPhone/iPad Base64->NSData->PDF how to?

Obj-C or Monotouch.Net C# answers are fine.
I have a Base64 string that is a PDF document received over a web service. I can get the NSData.
How do I take the NSData and save it as a PDF?
-- I get the NSData this way --
byte[] encodedDataAsBytes = System.Convert.FromBase64String (myBase64String);
string decoded = System.Text.Encoding.Unicode.GetString (encodedDataAsBytes);
NSData data = NSData.FromString (decoded, NSStringEncoding.ASCIIStringEncoding);
The simplest way to save it is probably to use NSData's writeToFile:options:error: method.
I found that using the .NET framework works better than trying to use the iOS framework for this problem. This will take any file and convert it to it's original then save it to the iPhone/iPad device. "path" is just a folder on the dev ice.
using (var f = System.IO.File.Create (path))
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String (Base64PDFString);
f.Write (encodedDataAsBytes, 0, encodedDataAsBytes.Length);
}
I'm working on a project where I recently had to accomplish the same thing you are describing. I get base64 encoded PDF files as strings from a .NET web service which need to be decoded to their original and saved as PDF files in the applications documents directory.
My solution was:
Use ASIHTTPRequest to communicate with the web service.
I then use TBXML to parse incoming xml and get the base64 as an NSString.
To decode the string I use a method from QSUtilities library called decodeBase64WithString.
Finally I save the result with NSData's writeToFile.
I have tested and successfully used this method with PDF files that are up to 25mb. I also had a couple of test runs with a 48mb file but that file made the decodeBase64WithString method take up too much memory and my app crashed. Haven't found a solution to this yet..
If you are working with multiple large files be sure to free up your memory once in a while. I got all my files in one loop in which I had to use my own nsautorelease pool and drain it at the end of the loop to free up any autoreleased objects.

Weird problem with UInt8 in iPhone (EXC_BAD_ACCESS)

I am developing an image processing application by converting image to bitmap. I am manipulating bits in bitmap to get the desired effect.
First time i process an image it gives the correct result on the second try it gives
EXC_BAD_ACCESS
debugger is showing:
dataref outofscope
My code is
CGImageRef img=previewImageView.image.CGImage;
NSLog(#" Image : %# ", previewImageView);
CFDataRef dataref=CopyImagePixels(img);
CFDataRef dataref1=CopyImagePixels(img);
//UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);
//UInt8 *original=(UInt8 *)CFDataGetBytePtr(dataref1);
UInt8 *data=nil;
data=(UInt8 *)CFDataGetBytePtr(dataref);
UInt8 *original=nil;
original=(UInt8 *)CFDataGetBytePtr(dataref1);
//original=data;
int length=CFDataGetLength(dataref);
Please help.........
A major cause of EXEC_BAD_ACCESS is from trying to access release objects.
To find out how to troubleshoot this, read this document: DebuggingAutoReleasePool
Even if you don't think you are "releasing auto-released objects", this will apply to you.
This method works extremely well.
In summary, this explains how to use Cocoa's NSZombie debugging class and the command line "malloc_history" tool to find exactly what released object has been accessed in you code.
It is useful to set a breakpoint on objc_exception_throw. That way the debugger should break when you get the EXC_BAD_ACCESS.
Instructions can be found here http://www.cocoadev.com/index.pl?DebuggingTechniques

Using .png files instead of using .ico files in windows programs

hi I have a collection of nice .png files....
meanwhile, I'm developing a win-based software and need some .ico files as icons for toolbar buttons and ....
Is there any way to use .png file as an icon ? or what?
Thank you
As a workaround you can use IrfanView to convert your *.png file to *.ico file (or any other image to ico) & use it.
http://www.irfanview.com/main_download_engl.htm
You can simply convert the images to ico files online Ico Convert.
If you are using .NET this is not a real problem for you, because afaik PNG support is already build in. You are probably talking about native C/C++ development with GDI/win32?
To my knowledge you will not accomplish this by simply using GDI. There are a couple of options where you can set ONE color as transparent then load a simple BMP/JPEG and do some BITMAP tricks however using ICO/GIF will be far easier for this.
What you are probably looking for is a working GDI+ example which will use a PNG with alpha channel? This is just an excerpt and I left out the whole mess loading external functions from a DLL part, but maybe this will help you:
static GpImage *background = NULL;
GDIPLOADIMAGEFROMSTREAM GdipLoadImageFromStream;
GDIPLUSSTARTUP GdiplusStartup;
GDIPPLUSSHUTDOWN GdiplusShutdown;
GDIPCREATEFROMHDC GdipCreateFromHDC;
GDIPDELETEGRAPHICS GdipDeleteGraphics;
GDIPDRAWIMAGEI GdipDrawImageI;
GDIPDRAWIMAGERECTI GdipDrawImageRectI;
GDIPLUS_STARTUP_INPUT GdiplusStartupInput;
void LoadPNG(GpImage **image, int resource, HMODULE hInstance)
{
HRSRC resrc;
LPSTREAM lpstr;
HGLOBAL hPng;
LPVOID fByte;
GpImage *img = NULL;
resrc = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(resource), TEXT("PNG"));
hPng = LoadResource(GetModuleHandle(NULL), resrc);
fByte = LockResource(hPng);
lpstr = SHCreateMemStream(fByte, 200000);
GdipLoadImageFromStream(lpstr, &img);
*image = img;
}
void CreateBack(HWND hWnd)
{
HDC memDC = NULL;
HDC hdc = NULL;
RECT rect;
DeleteObject(curBack);
GetClientRect(hWnd, &rect);
hdc = GetDC(hWnd);
memDC = CreateCompatibleDC(hdc);
curBack = CreateCompatibleBitmap(hdc, rect.right, 44);
SelectObject(memDC, curBack);
/* gdiplus - background*/ {
int e = 0;
GpGraphics *g;
GdipCreateFromHDC(memDC, &g);
GdipDrawImageRectI(g, background, e, 0, 971, 44);
GdipDeleteGraphics(g);
}
DeleteObject(memDC);
ReleaseDC(hWnd, hdc);
}
Just a quick note: This GDI+ stuff is really CPU/memory intensive for a couple of reasons. Although fun I did abandoned this approach in favor of gdi and simple BMPs.
If you're loading the images from a resource file, then no, you can't use PNGs, you have to use ICOs. Fortunately, there are a number of tools that can convert PNGs into ICOs, including ImageMagick (great for automation), and MSPaint as a lowest common denominator.
If you're loading image files at runtime, then you can load any type of image format you want (e.g. use libpng for loading PNGs), but you still have to convert them to icons internally before you can do interesting things with them, such as setting them as a window's icon. Once you've decoded the image data, it's not terribly difficult to convert it to the proper format, but it's not trivial, it just involves a lot of data mangling and strange structs and function calls from the Win32 API.