UIImage imageWithData returns nil - ios

I'm developing an iOS 5.0+ app with latest SDK.
On my app I have some C++ image recognition software, and I'm trying to save image recognized by this C++ functions.
I have this array to store image recognized:
int _detectedImages[NSMaxNumDetections ][NSPatchSize * NSPatchSize];
I call the C++ function this way:
numberOfDetections = nativeDetect(_resultImages);
And with the following code, I try to convert data from _detectedImages to UIImage.
for (int index = 0; index < numberOfDetections; index++)
{
if (_resultImages[index] != nil)
{
NSData* imageData = [NSData dataWithBytes:&_resultImages[index] length:sizeof(_resultImages[index])];
NSLog(#"##### Image data size: %d", [imageData length]);
UIImage* newImage = [UIImage imageWithData:imageData];
_lastDetectedSignImages[index] = newImage;
}
}
On the log I get this:
#### Image data size: 2304
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM setObject:atIndex:]: object cannot be nil'
*** First throw call stack:
newImage is nil but imageData has data.
Am I doing something wrong when I try to get an UIImage from that data?
The other possibility if that my C++ recognition software is doing something wrong, but I'm not going to show you that code here (sorry, it's copyrighted).
Or I'm not passing _resultImages correctly to nativeDetect.

Almost for sure your C++ images are in some bitmap format - RGBA etc. So your first task is to figure out what that format is (and the byte order too!). With that information you can use CGImageCreate() to create a CGImageRef. With that you can create a UIImage.

Related

raw CIFilter has nil outputImage

I get some DNG raw files and want to show them in iPhone. I use CIFilter to read the raw file, but when I try to get the outputImage, it returns nil.
The code looks like:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"1" withExtension:#"DNG"];
CIFilter *filter = nil;
filter = [CIFilter filterWithImageURL:url options:nil];
[filter setValue:#(2000) forKey:kCIInputNeutralTemperatureKey];
CIImage *imgCI = [filter.outputImage imageByApplyingFilter:#"CIVignette"];//5
UIImage *img = [UIImage imageWithCIImage:imgCI];
In the line 5, the fileter.outputImage is nil and hence the final img is nil. I check the DNG file in Mac and it shows it is Adobe Raw. I also print the filter in console:
CIRAWFilterImpl: inputRequestedSushiMode=nil inputNeutralChromaticityX=0.5342335533653005 inputNeutralChromaticityY=0.4233451399853654 inputNeutralTemperature=2000 inputNeutralTint=9.616524016108169 inputNeutralLocation=[] inputEV=0 inputBoost=1 inputDraftMode=nil inputScaleFactor=1 inputIgnoreOrientation=nil inputImageOrientation=1 inputEnableSharpening=1 inputEnableNoiseTracking=1 inputEnableVendorLensCorrection=0 inputNoiseReductionAmount=0 inputLuminanceNoiseReductionAmount=nil inputColorNoiseReductionAmount=nil inputNoiseReductionSharpnessAmount=nil inputNoiseReductionContrastAmount=nil inputNoiseReductionDetailAmount=nil inputMoireAmount=nil inputDecoderVersion=nil inputBoostShadowAmount=nil inputBias=nil inputBaselineExposure=nil inputDisableGamutMap=0 inputHueMagMR=nil inputHueMagRY=nil inputHueMagYG=nil inputHueMagGC=nil inputHueMagCB=nil inputHueMagBM=nil inputLinearSpaceFilter=nil>
The inputDecoderVersion seems nil which may cause the problem(but I'm not sure).
The DNG files are downloaded from Internet, and I also try the NEF files which has the same result.
Could anyone give some advice?
It should run in real device.
In simulator, it returns nil.

App Crashing as *** Assertion failure in +[SVGKImage imageWithSource:]

I am working with SVGKit in swift Project. I properly imported SVGKit Framework and 3rd party resources folder into my project by following (https://github.com/SVGKit/SVGKit).
I created a folder with sets of .svg images inside the project and i am using the following code to load image into testIcon(ImageView).
let lockImage: SVGKImage = SVGKImage(named: "LockIcon.svg")
let lockImageLocal: UIImage = lockImage.uiImage
testIcon.image = lockImageLocal
But i am receiving the following error,
** Assertion failure in +[SVGKImage imageWithSource:], /Users/fss/Downloads/SVGKit-
2.x/Source/SVGKImage.m:229
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not
satisfying: newSource != nil'
Please let me know how to mention path of .svg image(which is inside my project) to SVGKImage image class or how to resolve this problem.
please remove .svg from image name. This method named: doesn't need extension.
Or you can try this way too...
Swift 4
if let imagePath = Bundle.main.path(forResource: "imageName", ofType: "svg")
{
let lockImage: SVGKImage = SVGKImage(contentsOfFile: imagePath)
let lockImageLocal: UIImage = lockImage.uiImage
testIcon.image = lockImageLocal
}
Obj-c type
NSString *imagePath = [NSBundle mainBundle][ pathForResource:#"imageName" ofType:#"svg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

'PFObject values may not have class: NSConcreteValue'

App keeps crashing on this Method - trying to simply pin a Parse object to the local data store as outlined in the docs Parse docs:
func saveToBackground(title:String, description:String, coords:CLLocationCoordinate2D, image:UIImage, objectId:String, dateFound:String){
let imageData = image.jpeg(.low)
let imageFile = PFFile(name: "image.png", data: imageData!)
let foundObject = PFObject(className: "FoundObjects")
foundObject["title"] = title
foundObject["description"] = description
foundObject["coordinates"] = coords
foundObject["image"] = imageFile
foundObject["objectId"] = objectId
foundObject["dateFound"] = dateFound
foundObject.pinInBackground()
}
error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values may not have class: NSConcreteValue'
Any ideas anyone?
At least one of your values is an NSConcreteValue class, which can't be stored on a PFObject. Figure out which line is causing the issue by setting a breakpoint and stepping through, and make sure to cast that value to the expected class. Or you could cast all of these to their expected class, cover your bases.
Edit: As pointed out by MartynE23, the issue was actually the CLLocationCoordinate2D, which must be converted to a PFGeoPoint to add to a Parse Object

UIImage AsPNG and AsJPEG fails

I'm using MonoTouch and I have a UIImage (displayed in a UIImageView and it looks good) and I'm trying to convert it to NSData, but AsJPEG and AsPNG returns null. What can be the problem?
My code looks like this:
NSError err;
NSData imageData = CroppedImageView.Image.AsJPEG(); // imageData is null!
if (!imageData.Save ("tmp.png", true, out err)) {
Console.WriteLine("Saving of file failed: " + err.Description);
}
The AsJPEG method calls UIImageJPEGRepresentation and its return value is documented as:
A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
The is similar to many API in iOS (and OSX) where exception are not commonly used (and null is used to report some kind of error).
Anyway you should check your image dimensions and properties - they might give you an hint at something that would not translate into a JPEG bitmap.
Also since the NSData can represent a very large amount of memory you should try to limit it's life, e.g.:
using (NSData imageData = CroppedImageView.Image.AsJPEG ()) {
NSError err;
if (!imageData.Save ("tmp.jpg", true, out err)) {
Console.WriteLine("Saving of file failed: " + err.Description);
}
}
It looks like you are writing to a file in the current directory of the app, this is readonly.
You should use:
var path = System.IO.Path.GetTempFilename();
or
var path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "tmp.png");
Like you would do on other platforms, and use a file from there.
You can also use Environment.SpecialFolder.MyDocuments.
The AsJPEG returned null because the image size was too big (it was taken with an iPhone 5). After I Scaled it down by 2, it generates the data properly.

Exception creating a UIImage from an AssetURL in monotouch code

I am getting an exception on my second line of code here. I am calling this code from within ViewDidLoad. _assetURLs[page] (in this case page is 0), is returning a single assetURL that exists and was already retrieved.
UIImage img;
img = new UIImage(new MonoTouch.CoreImage.CIImage(_assetURLs[page]));
Any ideas?
Here is the exception:
(the exception is happening because of --> new UIImage(...) and not because of new MonoTouch.CoreImage.CIImage(_assetURLs[page])
{MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[UIImage initWithCIImage]: unrecognized selector sent to instance 0x107a1fc0 at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend_IntPtr (intptr,intptr,intptr) at MonoTouch.UIKit.UIImage..ctor (MonoTouch.CoreImage.CIImage ciImage) [0x00027] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIImage.g.cs:376 at ScrollViewPageViewExample.Viewer.LoadPageContent (Int32 page) [0x0002e] in /Users/user1/Dropbox/Dev/ScrollViewPageViewExample/ScrollViewPageViewExample/Viewer.cs:148 }
That an (already fixed, but not yet released) bug in MonoTouch.
You can use the overloaded constructor that accept a float and an UIImageOrientation as a workaround.
UIImage img = new UIImage (your_ciimage, 1.0f, UIImageOrientation.Up);

Resources