issue in generating image files in mac os x app cocoa - objective-c

-(void)processImage:(NSString*)inputPath:(int)imageWidth:(int)imageHeight:(NSString*)outputPath {
// NSImage * img = [NSImage imageNamed:inputPath];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:inputPath];
[image setSize: NSMakeSize(imageWidth,imageHeight)];
[[image TIFFRepresentation] writeToFile:outputPath atomically:NO];
NSLog(#"image file created");
}
- (IBAction)processImage:(id)sender {
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval is defined as double
NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
NSString *convertNumber = [formatter stringForObjectValue:timeStampObj];
NSLog(#"timeStampObj:: %#", convertNumber);
fileNameNumber = [[convertNumber stringByAppendingString:[self genRandStringLength:8]] retain];
int i; // Loop counter.
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
inputFilePath = [[files objectAtIndex:i] retain];
NSLog(#"filename::: %#", inputFilePath);
// Do something with the filename.
[selectedFile setStringValue:inputFilePath];
NSLog(#"selectedFile:::: %#", selectedFile);
}
NSLog(#"curdir:::::%#", inputFilePath);
NSString *aString = [[NSString stringWithFormat:#"%#%#%#", thumbnailDirPath , #"/" , fileNameNumber] retain];
fileNameJPG = [[aString stringByAppendingString:#"_small.jpg"] retain];
fileNameJPG1 = [[aString stringByAppendingString:#".jpg"] retain];
fileNameJPG2 = [[aString stringByAppendingString:#"_H.jpg"] retain];
[self processImage:inputFilePath: 66 :55 :fileNameJPG];
[self processImage:inputFilePath: 800 :600 :fileNameJPG1];
[self processImage:inputFilePath: 320 :240 :fileNameJPG2];
}
The issue I am facing is that the above code is generating 3 files with different names(as I have defined the name should be) having the same size of all 3 files but not with the dimensions or width/length I pass to the function.
What can be the issue ?

NSImage objects are immutable. So image is not modified when you change its size.
You should use something like the following code (adapted from here).
-(void)saveImageAtPath:(NSString*)sourcePath toPath:(NSString*)targetPath withWidth:(int)targetWidth andHeight:(int)targetHeight
{
NSImage *sourceImage = [[NSImage alloc] initWithContentsOfFile:sourcePath];
NSImage *targetImage = [[NSImage alloc] initWithSize: NSMakeSize(targetWidth, targetHeight)];
NSSize sourceSize = [sourceImage size];
NSRect sourceRect = NSMakeRect(0, 0, sourceSize.width, sourceSize.height);
NSRect targetRect = NSMakeRect(0, 0, targetWidth, targetWidth);
[targetImage lockFocus];
[sourceImage drawInRect:targetRect fromRect:sourceRect operation: NSCompositeSourceOver fraction: 1.0];
[targetImage unlockFocus];
[[targetImage TIFFRepresentation] writeToFile:targetPath atomically:NO];
NSLog(#"image file created");
[sourceImage release];
[targetImage release];
}

Related

Get all Images from Live Photo

I want to get a NSArray with all the UIImage from a Live Photo to create a GIF of that. I tried to make screenshots while animating the live photo but it doesn't work.
Can anyone help me?
Thanks!
First step, you need convert a Live Photo to Video, using this:
PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes,
toFile: fileURL, options: nil, completionHandler:
{
// Video file has been written to path specified via fileURL
}
Finally, using this library to convert this to GIF, or you can search in google for another way: https://github.com/NSRare/NSGIF
Hope this will help you.
This is what I did to achieve the same thing as you requested.
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:#"mediaType == %d", PHAssetMediaTypeImage];
options.predicate = [NSPredicate predicateWithFormat:#"mediaSubtype == %d", PHAssetMediaSubtypePhotoLive];
options.includeAllBurstAssets = NO;
PHFetchResult *allLivePhotos = [PHAsset fetchAssetsWithOptions:options];
NSLog(#"Get total live count : %ld",(unsigned long)allLivePhotos.count);
NSMutableArray *arrAllLiveImagesGroups = [NSMutableArray array];
for (PHAsset *asset in allLivePhotos) {
[asset requestContentEditingInputWithOptions:nil
completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *urlMov = [contentEditingInput.livePhoto valueForKey:#"videoURL"];
NSMutableArray *arrLive = [NSMutableArray array];
NSMutableArray *arrSingleLiveImagesGroup = [NSMutableArray array];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlMov options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.requestedTimeToleranceAfter = kCMTimeZero;
generator.requestedTimeToleranceBefore = kCMTimeZero;
for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) * 5 ; i++){
#autoreleasepool {
CMTime time = CMTimeMake(i, 5);
NSError *err;
CMTime actualTime;
CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image scale:1.0 orientation:UIImageOrientationDown];
[arrLive addObject:generatedImage];
CGImageRelease(image);
}
}
[arrSingleLiveImagesGroup addObject:arrLive];
[arrAllLiveImagesGroups addObject:arrSingleLiveImagesGroup];
}];
}

saving thumbnail image to directory

Hi I'm going to save thumbnail image get from video to directory with the time duration and with video icon just like
How it could be possible to save like this.
I'm using the code to save image to directory and later I'll display it in GridView.
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
{
// Getting Time Duration of video file
NSURL* videoUrl = alassetRep.url;
AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
CMTime time = [avUrl duration];
NSUInteger dTotalSeconds = ceil(time.value/time.timescale);
NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);
NSString *videoDurationText = [NSString stringWithFormat:#"%02i:%02i", dMinutes, dSeconds];
NSLog(#"%#",videoDurationText);
/************************************Low Resolution Images ******************************************/
UIImage *image = [UIImage imageWithCGImage:[alassetRep fullResolutionImage]];
UIImage *thumbImage = [self imageWithImage:image scaledToSize:CGSizeMake(50, 50)];
NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, 0.8);
NSString *thumbOriginalPath = [NSString stringWithFormat:#"SMALL_IMAGE_%d_%d.jpg",(int)currentDate,i];
NSString* thumbImagePath = [DucPath stringByAppendingPathComponent:thumbOriginalPath];
NSLog(#"Image path At Save Time:%#",thumbImagePath);
[thumbImageData writeToFile:thumbImagePath atomically:YES];
[pMediaArray addObject:thumbOriginalPath];
}
Yes I've found solution....
if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
{
unsigned long DataSize = (unsigned long)[alassetRep size];
Byte *buffer = (Byte*)malloc(DataSize);
NSUInteger buffered = (NSUInteger)[alassetRep getBytes:buffer fromOffset:0.0 length:(NSUInteger)alassetRep.size error:nil];
NSData *videoData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
NSString* newVideoName = [NSString stringWithFormat:#"video_%d_%d.mov",(int)currentDate,i];
NSString* newVideoPath = [DucPath stringByAppendingPathComponent:newVideoName];
[videoData writeToFile:newVideoPath atomically:YES];
[pImageMediaArray addObject:newVideoName];
NSLog(#"%#",newVideoName);
// Getting Time Duration of video file
NSURL* videoUrl = alassetRep.url;
AVURLAsset *avUrl = [AVURLAsset assetWithURL:videoUrl];
CMTime time = [avUrl duration];
NSUInteger dTotalSeconds = ceil(time.value/time.timescale);
NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);
NSString *videoDurationText = [NSString stringWithFormat:#"%02i:%02i", dMinutes, dSeconds];
/************************************Low Resolution Images ******************************************/
UIImage *lowResImage = [UIImage imageWithCGImage:[alassetRep fullResolutionImage]];
UIImage *thumbImage = [self imageWithImage:lowResImage scaledToSize:CGSizeMake(50, 50)];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:(CGRect){ CGPointZero, thumbImage.size}];
imageView.image = thumbImage;
UIImageView* timeView = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.frame.size.width - 35, imageView.frame.size.height - 20, 30, 15)];
timeView.image = [UIImage imageNamed:#"screen2-videoTime-icon.png"];
UIImageView* videoIcon = [[UIImageView alloc] initWithFrame:CGRectMake(imageView.frame.origin.x+5, imageView.frame.size.height - 15, 15, 10)];
videoIcon.image = [UIImage imageNamed:#"screen2-video-icon.png"];
UILabel* timelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 15)];
timelabel.textColor = [UIColor whiteColor];
timelabel.text = videoDurationText;
timelabel.textAlignment = NSTextAlignmentCenter;
timelabel.font = [UIFont fontWithName:#"Helvetica" size: 10.0];
[timeView addSubview:timelabel];
[timeView bringSubviewToFront:timelabel];
[imageView addSubview:videoIcon];
[imageView addSubview:timeView];
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *thumbImageData = UIImageJPEGRepresentation(screenShot, 0.8);
NSString *thumbOriginalPath = [NSString stringWithFormat:#"SMALL_IMAGE_%d_%d.jpg",(int)currentDate,i];
NSString* thumbImagePath = [DucPath stringByAppendingPathComponent:thumbOriginalPath];
NSLog(#"Image path At Save Time:%#",thumbImagePath);
[thumbImageData writeToFile:thumbImagePath atomically:YES];
[pMediaArray addObject:thumbOriginalPath];
}

Creating an animated GIF in Cocoa - defining frame type

I've been able to adapt some code found on SO to produce an animated GIF from the "screenshots" of my view, but the results are unpredictable. GIF frames are sometimes full images, full frames ("replace" mode, as GIMP marks it), other times are just a "diff" from previous layer ("combine" mode).
From what I've seen, when there are fewer and/or smaller frames involved, the CG writes the GIF in "combine" mode, but failing to get the colors right. Actually, the moving parts are colored correctly, the background is wrong.
When CG saves the GIF as full frames, the colors are ok. The file size is larger, but hey, obviously you cannot have the best of both worlds. :)
Is there a way to either:
a) force CG to create "full frames" when saving the GIF
b) fix the colors (color table?)
What I do is (ARC mode):
capture the visible part of the view with
[[scrollView contentView] dataWithPDFInsideRect:[[scrollView contentView] visibleRect]];
convert and resize it to NSImageBitmapRep of PNG type
-(NSMutableDictionary*) pngImageProps:(int)quality {
NSMutableDictionary *pngImageProps;
pngImageProps = [[NSMutableDictionary alloc] init];
[pngImageProps setValue:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
double compressionF = 1;
[pngImageProps setValue:[NSNumber numberWithFloat:compressionF] forKey:NSImageCompressionFactor];
return pngImageProps;
}
-(NSData*) resizeImageToData:(NSData*)data toDimX:(int)xdim andDimY:(int)ydim withQuality:(int)quality{
NSImage *image = [[NSImage alloc] initWithData:data];
NSRect inRect = NSZeroRect;
inRect.size = [image size];
NSRect outRect = NSMakeRect(0, 0, xdim, ydim);
NSImage *outImage = [[NSImage alloc] initWithSize:outRect.size];
[outImage lockFocus];
[image drawInRect:outRect fromRect:inRect operation:NSCompositeCopy fraction:1];
NSBitmapImageRep* bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:outRect];
[outImage unlockFocus];
NSMutableDictionary *imageProps = [self pngImageProps:quality];
NSData* imageData = [bitmapRep representationUsingType:NSPNGFileType properties:imageProps];
return [imageData copy];
}
get the array of BitmapReps and create the GIF
-(CGImageRef) pngRepDataToCgImageRef:(NSData*)data {
CFDataRef imgData = (__bridge CFDataRef)data;
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData (imgData);
CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
return image;
}
////////// create GIF from
NSArray *images; // holds all BitmapReps
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pot],
kUTTypeGIF,
allImages,
NULL);
// set frame delay
NSDictionary *frameProperties = [NSDictionary
dictionaryWithObject:[NSDictionary
dictionaryWithObject:[NSNumber numberWithFloat:0.2f]
forKey:(NSString *) kCGImagePropertyGIFDelayTime]
forKey:(NSString *) kCGImagePropertyGIFDictionary];
// set gif color properties
NSMutableDictionary *gifPropsDict = [[NSMutableDictionary alloc] init];
[gifPropsDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
[gifPropsDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
// set gif loop
NSDictionary *gifProperties = [NSDictionary
dictionaryWithObject:gifPropsDict
forKey:(NSString *) kCGImagePropertyGIFDictionary];
// loop through frames and add them to GIF
for (int i=0; i < [images count]; i++) {
NSData *imageData = [images objectAtIndex:i];
CGImageRef imageRef = [self pngRepDataToCgImageRef:imageData];
CGImageDestinationAddImage(destination, imageRef, (__bridge CFDictionaryRef) (frameProperties));
}
// save the GIF
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)(gifProperties));
CGImageDestinationFinalize(destination);
CFRelease(destination);
I've checked the ImageBitmapReps, when saved as PNG individually, they are just fine.
As I understood, the color tables should be handled by CG or am I responsible to produce the dithered colors? How to do that?
Even when doing the same animation repeatedly, the GIFs produced may vary.
This is a single BitmapRep
(source: andraz.eu)
And this is the GIF with the invalid colors ("combine" mode)
(source: andraz.eu)
I read your code. Please double check the "allImages" while you are creating the CGImageDestinationRef, and the "[images count]".
the follow test code works fine:
NSDictionary *prep = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.2f] forKey:(NSString *) kCGImagePropertyGIFDelayTime] forKey:(NSString *) kCGImagePropertyGIFDictionary];
CGImageDestinationRef dst = CGImageDestinationCreateWithURL((__bridge CFURLRef)(fileURL), kUTTypeGIF, [filesArray count], nil);
for (int i=0;i<[filesArray count];i++)
{
//load anImage from array
...
CGImageRef imageRef=[anImage CGImageForProposedRect:nil context:nil hints:nil];
CGImageDestinationAddImage(dst, imageRef,(__bridge CFDictionaryRef)(prep));
}
bool fileSave = CGImageDestinationFinalize(dst);
CFRelease(dst);

Resizing image and saving it to the specified directory path in Cocoa

Using this code I am trying to resize the selected image and then want to save it to a specific path:
-(void)processImage:(NSString*)inputPath:(int)imageWidth:(int)imageHeight:(NSString*)outputPath {
NSImage * img = [NSImage imageNamed:inputPath];
[img setSize: NSMakeSize(imageWidth,imageHeight)];
}
-(void)startProcessingImages {
int i; // Loop counter.
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
inputFilePath = [[files objectAtIndex:i] retain];
NSLog(#"filename::: %#", inputFilePath);
// Do something with the filename.
[selectedFile setStringValue:inputFilePath];
NSLog(#"selectedFile:::: %#", selectedFile);
}
NSLog(#"curdir:::::%#", inputFilePath);
NSString *aString = [[NSString stringWithFormat:#"%#%#%#", thumbnailDirPath , #"/" , fileNameNumber] retain];
fileNameJPG = [[aString stringByAppendingString:#".jpg"] retain];
[self processImage:inputFilePath: 66 :55 :thumbnailDirPath];
[self processImage:inputFilePath: 800 :600 :thumbnailDirPath];
[self processImage:inputFilePath: 320 :240 :thumbnailDirPath];
}
My issue is I am not getting that how to save it to thumbnailDirPath.
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.8] forKey:NSImageCompressionFactor];
NSData *tiffData = [img TIFFRepresentation];
NSData *JPEGData = [[NSBitmapImageRep imageRepWithData:tiffData] representationUsingType:NSJPEGFileType properties:options];
NSError *anError;
if (![JPEGData outputPath options:0 error:&anError])
MyLog(#"Error saving image: %# to: %#", anError, outputPath);
Check the documentation for NSJPEGFileType as it will show you the other foramt options for saving, such as PNG.
you should do export your image into file.
currently I only see how to store the TIFF image.
[[img TIFFRepresentation] writeToFile:outputPathName atomacally:NO];
Where outputPathName is the path with file name for your thumbnail file.

memory leak in iPad app

I've been working a product display app but it has a memory leak that causes it to crash after too many categories have been loaded up. The app works via a SplitViewController that lists the categories down the left and, once tapped, the product images show in the detailViewController on the right.
Selecting category after category eventually crashes the app.
I've used the instruments -> Leaks tool to trace the problem and I get told that NSString appendString is a leak. The number of strings leaked seems to match the number of products in the category selected so I'm guessing one of my loops holds the problem but, after playing around with AutoreleasePools I haven't solved it yet.
My code:
This method is called when the category is selected and parses an XML document
- (NSMutableArray*) processXML{
//NSAutoreleasePool *pool4 = [[NSAutoreleasePool alloc] init];
// Initialize the productEntries MutableArray declared in the header
products = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString *documentsDirectory = [[NSMutableString stringWithFormat:#"%#", [paths objectAtIndex: 0]] autorelease];
// paths to save inputs to
NSString *productsFile = [documentsDirectory stringByAppendingFormat: #"/products2.xml"];
NSData *data = [NSData dataWithContentsOfFile: productsFile];
// Create a new rssParser object based on the TouchXML "CXMLDocument" class, this is the object that actually grabs and processes the RSS data
NSError *error = nil;
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithData:(NSData *)data encoding:NSUTF8StringEncoding options:0 error:&error] autorelease];
// Create a new Array object to be used with the looping of the results from the rssParser
NSArray *resultNodes = NULL;
//NSString *xPathStart, *xPathEnd, *category, *finalStr;
NSString *xPathStart = [[NSString stringWithFormat:#""] autorelease];
NSString *xPathEnd = [[NSString stringWithFormat:#""] autorelease];
NSString *category = [[NSString stringWithFormat:#""] autorelease];
NSString *finalStr = [[NSString stringWithFormat:#""] autorelease];
NSString *detailStr = [[NSString stringWithFormat: detailItem] autorelease];
// category to be parsed - build up xPath expression
if([detailStr isEqualToString: #"On Order Stock"]) {
xPathStart = #"/products/product[instock='2";
xPathEnd = #"']";
finalStr = [NSString stringWithFormat:#"%#%#", xPathStart, xPathEnd];
} else {
xPathStart = #"/products/product[category='";
category = detailItem;
xPathEnd = #"']";
finalStr = [NSString stringWithFormat:#"%#%#%#", xPathStart, category, xPathEnd];
}
resultNodes = [rssParser nodesForXPath: finalStr error:nil];
// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
Product *productItem = [[Product alloc] init];
[productItem setCode: [[[resultElement childAtIndex: 1] stringValue] autorelease]];
[productItem setImage: [[[resultElement childAtIndex: 5] stringValue] autorelease]];
// Add the product object to the global productEntries Array so that the view can access it.
[products addObject: productItem];
[productItem release];
}
//[pool4 release];
return products;
}
As you can see I went a little crazy with autoReealse on my strings. The other code segment that displays the images could be the problem although Leaks does mention processXML directly.
- (void) displayImages:(NSMutableArray *)anArray {
// create scrollView object
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100)];
scrollView.pagingEnabled = NO;
scrollView.scrollEnabled = YES;
scrollView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
scrollView.userInteractionEnabled = YES;
//create info area below scrollView
infoView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 100)];
[infoView setContentSize:CGSizeMake(self.view.frame.size.width, 100)];
infoView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
infoView.scrollEnabled = NO;
[barcodeImgView setImage:[UIImage imageNamed:#"barcode2.jpg"]];
[infoView addSubview:codeLbl];
[infoView addSubview:nameLbl];
[infoView addSubview:priceLbl];
[infoView addSubview:dimensionsLbl];
[infoView addSubview:stockLbl];
[infoView addSubview:commentsLbl];
[infoView addSubview:barcodeImgView];
infoView.userInteractionEnabled = YES;
[codeLbl setText:[[NSString stringWithFormat:#""] autorelease]];
[nameLbl setText:[[NSString stringWithFormat:#""] autorelease]];
[priceLbl setText:[[NSString stringWithFormat:#""] autorelease]];
[commentsLbl setText:[[NSString stringWithFormat:#""] autorelease]];
[stockLbl setText:[[NSString stringWithFormat:#""] autorelease]];
[dimensionsLbl setText:[[NSString stringWithFormat:#""] autorelease]];
// hold x and y of each image
int x = 30;
int y = 50;
int noOfImages = [anArray count];
int maxRowWidth = (noOfImages / 3) + 1;
int xcount = 0; // position across the row, reset to zero and drop image down when equal to (noOfImages / 3) + 1
//NSAutoreleasePool *displayPool = [[NSAutoreleasePool alloc] init];
for(int i = 0; i < noOfImages; i++) {
// declare Product object to hold items in anArray
Product *prod = [[Product alloc] init];
prod = [anArray objectAtIndex: i];
// try for image in Documents folder, later checks it exists and if not uses Resource location
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString *documentsDirectory = [[NSString stringWithFormat:#"%#", [paths objectAtIndex: 0]] autorelease];;
// paths to save inputs to
NSString *imgName = [[NSString stringWithFormat:#"%#/%#", documentsDirectory, [prod image]] autorelease];
NSString *productName = [[NSString stringWithFormat:#"%#", [prod code]] autorelease];
// create and size image
UIImage *image = [UIImage imageWithContentsOfFile: imgName];
// set up button
UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(imageButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[button setTitle:productName forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize: 0];
[button setTitleColor: [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1] forState: UIControlStateNormal];
CGSize imageSize = image.size;
CGFloat height = imageSize.height;
CGFloat width = imageSize.width;
CGFloat ratio = 160 / width; // get ratio to divide height by
UIGraphicsBeginImageContext(CGSizeMake((height * ratio),160));
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect: CGRectMake(0, 0, height * ratio, 160)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// create frame for image
CGRect newFrame = CGRectMake(x, y, 160,160);
UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y - 20, 170, 20)];
codeLabel.text = productName;
codeLabel.textColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
codeLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
[button setFrame: newFrame];
[button setBackgroundImage:smallImage forState:UIControlStateNormal];
[scrollView setContentSize:CGSizeMake((maxRowWidth * 160) + 160,self.view.frame.size.height - 100)];
[self.scrollView addSubview:button];
[self.scrollView addSubview:codeLabel];
xcount++;
x = x + 170; // move across the page
if(xcount == maxRowWidth) {
y = y + 210; // move down the screen for the next row
x = 30; // reset x to left of screen
xcount = 0; // reset xcount;
}
[prod release];
}
//[displayPool release];
[self.view addSubview: scrollView];
[self.view addSubview: infoView];
[scrollView release];
[infoView release];
[pool release];
}
By the way, pool is an autoreleasePool defined in the h file for the class.
I would really appreciate any specific help regarding my code or general tips on what could be wrong.
I see a few things wrong:
As was mentioned in the comments, you're abusing -autorelease in ways which make grown men cry and that which will crash your app.
-processXML is returning an owned object. You're allocating products and returning it. This breaks convention, because the method name does not begin with new or alloc, and does not contain copy. You should return [products autorelease]; instead. However, even that is shady, because since products isn't declared locally, it's probably an instance variable. In that case, what happens if processXML gets invoked multiple times? You have an owned object referenced by the instance variable, and suddenly you overwrite that reference with a new one... = memory leak.
Every time someone does MyClass * object = [[MyClass alloc] init]; object = [something thatReturnsAMyClass];, a kitten dies. If you then do [object release];, a second dies for good measure. This is a terrible, terrible memory leak (and a likely crash). You're allocating a new object and then immediately throwing it away but never releasing it. That you do this suggests you don't really get what a pointer is. I suggest reading "Everything you need to know about pointers in C"
On a lighter note, you should check out -[NSString stringByAppendingPathComponent:]. NSString has a bunch of really nice methods for dealing with paths.
I hope I don't come off as too harsh. :)
Some while ago at another post somebody said that one should read about the memory management and I was actually thinking that this answer is not really right. What is wrong with some trial and error and learning by doing. But after I had my painful experiences with memory I must admit that this guy was right. Take the time. Go and read the chapter on memory management in the apple documentation.
As above stated already you should not autorelease an object you do not own. But this might not cause the trouble. You can next to the instruments use Build+Analyze in the Build menu. This will help you to find out more.
Basically you need to release objects you create which you own (which one you own is in the documentation, basically those created with "alloc" and some more). If you cannot release them you assign them to the autorelease pool. This is the case with the "products" you return from processXML. When is the autorelease pool drained? This is when the next time the framework of the application is back in control (I think it was called run-loop or something). This can be a while and so you should not open to much objects which are assigned to an autorelease pool.
So to help you really read that chapter: memory management programming guide