Cannot apply filter to image - cocoa

I need to apply CIZoomBlur with default values to image. I'm doing it with the following code:
NSURL * url = [NSURL fileURLWithPath: #"galaxy.png"];
CIImage * image = [CIImage imageWithContentsOfURL: url];
CIContext *context = [[CIContext alloc] init];
CIFilter *filter = [CIFilter filterWithName:#"CIZoomBlur"];
[filter setValue:image forKey:kCIInputImageKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
NSImage *image2 = [[NSImage alloc] initWithCGImage:cgImage size:NSSizeFromCGSize(CGSizeMake(640, 1136))];
self.imageView.image = image2;
In log I can see:
/SourceCache/CoreImage/CoreImage-9.2.8/API/CIFilter.mm:594: CIZoomBlur apply:: Caught exception: CIZoomBlur: id: nil value for argument #0 (src)
Image is located inside project. Any ideas?

The right code is:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"galaxy" ofType:#"png"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
CIImage * image = [CIImage imageWithData:data];
CIContext *context = [[CIContext alloc] init];
CIFilter *filter = [CIFilter filterWithName:#"CIZoomBlur"];
[filter setValue:image forKey:kCIInputImageKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];
NSImage *image2 = [[NSImage alloc] initWithCGImage:cgImage size:NSSizeFromCGSize(CGSizeMake(640, 1136))];
self.imageView.image = image2;
If the file is located within the app bundle then using NSURL fileURLWithPath will not locate it. The image must be nil. Thanks to trojanfoe

Related

Using DLImageLoader-iOS

Is there anyone that can help me combine this
https://github.com/AndreyLunevich/DLImageLoader-iOS
With my code below... I am not understanding as my images are loaded from an NSString in core data and not a URL (maybe I cannot use this helper file)
My code is:
SCTableViewSection *Section3 = [self.tableViewModel sectionAtIndex:0];
Section3.cellActions.lazyLoad = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
DLImageView *imageView = (DLImageView *)[cell viewWithTag:1];
NSString *imageString = (NSString *)[cell.boundObject valueForKey:#"thumbnail"];
NSString *fullName = [NSString stringWithFormat:#"Documents/%#", imageString];
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:fullName];
UIImage *originalImage = [UIImage imageWithContentsOfFile:imagePath];
NSLog(#"added image from lazyload");
// size of actual thumbnail to display in cell
CGSize destinationSize = CGSizeMake(154,95); // size of actual thumbnail
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIView transitionWithView:imageView
duration:2.25f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageView.image = thumbnail;
} completion:nil];
};

cocoa nsimage does not get saved

I'm new to cocoa and programming, sorry if this is something basic.
I'm using ARC. I have an NSImageView component, which is controlled by DropZone class. I drag & drop an image into it, but once I try to call in the scalling method I got, it tells me that the ": ImageIO: CGImageSourceCreateWithData data parameter is nil" I assume I'm doing something wrong, just don't know what yet.
Here's my method in DropZone.m
- (void)scaleIcons:(NSString *)outputPath{
NSImage *anImage;
NSSize imageSize;
NSString *finalPath;
anImage = [[NSImage alloc]init];
anImage = image;
imageSize = [anImage size];
imageSize.width = 512;
imageSize.height = 512;
[anImage setSize:imageSize];
finalPath = [outputPath stringByAppendingString:#"/icon_512x512.png"];
NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
[dataToWrite writeToFile:finalPath atomically:NO];
}
The DropZone.h
#interface DropZone : NSImageView <NSDraggingDestination>{
NSImage *image;
}
- (void)scaleIcons:(NSString *)outputPath;
#end
And here's how I call it it my AppDelegate.m
- (IBAction)createIconButtonClicked:(id)sender {
NSFileManager *filemgr;
NSString *tempDir;
filemgr = [NSFileManager defaultManager];
tempDir = [pathString stringByAppendingString:#"/icon.iconset"];
NSURL *newDir = [NSURL fileURLWithPath:tempDir];
[filemgr createDirectoryAtURL: newDir withIntermediateDirectories:YES attributes: nil error:nil];
DropZone *myZone = [[DropZone alloc] init];
[myZone scaleIcons:tempDir];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Done!"];
[alert runModal];
}
the image get's pulled from the pastebaord:
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
if ([NSImage canInitWithPasteboard:[sender draggingPasteboard]]) {
image = [[NSImage alloc]initWithPasteboard:[sender draggingPasteboard]];
[self setImage:image];
[self setImageAlignment: NSImageAlignCenter];
}
return YES;
}
For some reason my "image" gets lost. Could somebody help?
The problem is that you're creating a new instance of DropZone in your app delegate, but I'm assuming that you created the image view in IB and changed its class to DropZone. Is that correct? If so, you need to have an IBOutlet in the app delegate connected to the image view in IB, and have this:
[self.iv scaleIcons:tempDir];
where iv is my IBOutlet.
Without knowing where "image" comes from, I would say your problem is in these lines...
anImage = [[NSImage alloc]init];
anImage = image;
You should just grab the image from the imageView...
anImage = [myImageView image];

How do I convert an annotated PDF to an image?

I have an annotated PDF and want to convert it to image. The pdf is successfully getting converted to an image but annotations are missing in the conversion.
This is the code:
NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfFile:PdfFilePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
[img setCurrentPage:0];
NSImage *temp = [[NSImage alloc] init];
[temp addRepresentation:img];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSString *pageName = [NSString stringWithFormat:#"Page1_%d.jpeg", 0];
NSString *imgFilePath = [NSString stringWithFormat:#"%#/%#",#"/Users/Desktop/PDFImages", pageName];
[fileManager createFileAtPath:imgFilePath contents:finalData attributes:nil];
Is there a different way to store the annotated pdf to image?
The issue is resolved using image lock and unlock feature.
NSSize pageBounds1;
pageBounds1 = [page1 boundsForBox: [_pdfView displayBox]].size;
NSImage *temp = [[NSImage alloc] initWithSize:pageBounds1];
[temp lockFocus];
[page1 drawWithBox:kPDFDisplayBoxMediaBox];
[temp unlockFocus];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSString *pageName = [NSString stringWithFormat:#"Page1_%d.jpg", pageIndex];
NSString *imgFilePath = [NSString stringWithFormat:#"%#/%#",pathForImages, pageName];
[fileManager createFileAtPath:imgFilePath contents:finalData attributes:nil];

How to add a jpg or png image to an UIImage and then display in a UIImageView?

UIImage *img = [[UIImage alloc] initWithContentsOfFile:#"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];
I have already hooked up the connections for the UIImageView (_headersView) in IB. The image I want to load is in my project tree structure as well.
Am I creating the UIImage the right way?
If the file is in your bundle you can just use
UIImage *img = [UIImage imageNamed:#"contactHeader"];
_headersView = [[UIImageView alloc] initWithImage:img];
If the image is in your documents directory you can use
// Get the path to your documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add the file to the end of the documents path
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"contactHeader.png"];
UIImage *img = [[UIImage alloc] imageWithContentsOfFile:filePath];
_headersView = [[UIImageView alloc] initWithImage:img];
[img release]; img = nil;
try this
-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:#"png"];
return path;
path = nil;
}
self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:#"123"]];

desktop wallpaper [duplicate]

I am trying to change the desktop image; the procedure I've come up with is below. The first time this code is run, the resized image is displayed on screen as wallpaper, but the next time there is no reaction. What am I doing wrong?
-(IBAction)click:(id)sender
{
NSData *sourceData;
NSError *error;
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
screenArray = [NSScreen screens];
screenCount = [screenArray count];
unsigned index = 0;
for (index; index < screenCount; index++)
{
screenz = [screenArray objectAtIndex: index];
screenRect = [screenz visibleFrame];
}
NSLog(#"%fx%f",screenRect.size.width, screenRect.size.height);
arrCatDetails = [strCatDetails componentsSeparatedByString:appDelegate.strColDelimiter];
NSString *imageURL = [NSString stringWithFormat:#"upload/product/image/%#_%#_%d.jpg",[arrCatDetails objectAtIndex:0],appDelegate.str104by157Name,iSelectedImgIndex];
NSString *ima = [imageURL lastPathComponent];
NSString *str = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *dataFilePath = [str stringByAppendingPathComponent:ima];
NSString *imagePath = [NSString stringWithFormat:#"file://localhost%#",dataFilePath];
NSURL *url = [[NSURL alloc] init];
url = [NSURL URLWithString:imagePath];
sourceData = [NSData dataWithContentsOfURL:url];
sourceImage = [[NSImage alloc] initWithData: sourceData];
resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(screenRect.size.width, screenRect.size.height)];
NSSize originalSize = [sourceImage size];
[resizedImage lockFocus];
[sourceImage drawInRect: NSMakeRect(0, 0, screenRect.size.width, screenRect.size.height) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];
NSData *resizedData = [resizedImage TIFFRepresentation];
NSBitmapImageRep* theImageRepresentation = [NSBitmapImageRep imageRepWithData:resizedData];
newimage = #"editwall.jpg";
newFilePath = [str stringByAppendingPathComponent:newimage];
NSData* theImageData = [theImageRepresentation representationUsingType:NSJPEGFileType properties:nil];
[theImageData writeToFile: newFilePath atomically: YES];
if([filemgr fileExistsAtPath:newFilePath] == YES)
{
imagePath1 = [NSString stringWithFormat:#"file://localhost%#",newFilePath];
urlz = [NSURL URLWithString:imagePath1];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:nil, NSWorkspaceDesktopImageFillColorKey, [NSNumber numberWithBool:NO], NSWorkspaceDesktopImageAllowClippingKey, [NSNumber numberWithInteger:NSImageScaleProportionallyUpOrDown], NSWorkspaceDesktopImageScalingKey, nil];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:urlz forScreen:[[NSScreen screens] lastObject] options:options error:&error];
}
else
{
NSLog(#"No");
}
[sourceImage release];
[resizedImage release];
}
Why not try -[NSWorkspace setDesktopImageURL:forScreen:options:error:]? Apple has a sample project called DesktopImage to give you some idea how to use it.
Edit (after reading your code more carefully):
The problem you're having may be because of your call to +[NSDictionary dictionaryWithObjectsAndKeys:] See the nil at the end of the list of arguments? That's how you tell NSDictionary that your argument list is done. You can't put nil in the list, because it will stop reading the list at that point. If you want to specify a key that has no value, you have to use [NSNull null].
An aside: you've got a memory management issue in your code:
// allocates memory for an NSURL
NSURL * url = [[NSURL alloc] init];
// allocates more memory for an NSURL, and leaks
// the earlier allocation
url = [NSURL URLWithString:imagePath];
Just do one or the other:
// If you do it this way, you will have to call
// [url release] later
NSURL * url = [[NSURL alloc] initWithString:imagePath];
// This memory will be released automatically
NSURL * otherUrl = [NSURL URLWithString:imagePath];

Resources