merge two images

61 views
Skip to first unread message

Asad Ullah

unread,
Nov 18, 2009, 11:43:44 PM11/18/09
to iphonesdkd...@googlegroups.com
Hello All 

i want to add/merge two images into one image and then save the final
image.
like for example i want to add frames to an image, frames are also images.
i will add/merge two images and then get the final one image.


Thanks in advance.

Cantek Batur

unread,
Nov 23, 2009, 7:26:25 AM11/23/09
to iphonesdkd...@googlegroups.com
Hi again,

I have the following array;

NSMutableArray *arrayStocks = [[NSArray alloc] initWithObjects:@"TG:ED:DELL", @"WE:DR:HHG:UU:TRF:DEF:WWS", @"WW:A:SD:QA", @"ER:DW", @"", @"", @"", @"", @"G", nil];
self.listOfItems = arrayStocks;
[arrayStocks release]; 

and I'd like to create another array from this one that holds the number of characters seperated by " : " .  The second array should look like

NSMutableArray *arrayCount = [[NSArray allocinitWithObjects:@"3"@"7"@"4"@"2"@"0"@"0"@0""@"0"@"1"nil];
self.itemCount = arrayCount;
[arrayCount release]; 


any help is appreciated, 

Thanks

Lucas Neiva

unread,
Nov 23, 2009, 6:32:39 AM11/23/09
to iphonesdkd...@googlegroups.com
Loop through the arrayStocks Array, use "componentsSeparatedByString" to split the Strings, use "count" to get the count and add them the array arrayCounts:

NSMutableArray *arrayCounts= [[NSMutableArray alloc] initWithCapacity:[arrayStocks count]];
for (NSString *string in arrayStocks) {
NSArray *stringItems= [string componentsSeparatedByString:@":"];
[arrayCounts addObject: [NSString stringWithFormat:@"%d", [stringItems count]; /* You could consider using NSNumber instead of NSString here */
}

NOTE: I did not compile or test this. But I'm sure it's something similar to this.
> --
>
> You received this message because you are subscribed to the Google Groups
> "iPhone SDK Development" group.
> To post to this group, send email to iphonesdkd...@googlegroups.com.
> To unsubscribe from this group, send email to
> iphonesdkdevelop...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/iphonesdkdevelopment?hl=.
>

Cantek Batur

unread,
Nov 23, 2009, 1:55:01 PM11/23/09
to iphonesdkd...@googlegroups.com
Thanks Lucas,

I made it work exactly this way, but the problem is when there is 1 value it returns 1, when there is no value it again returns 1. 

Lucas Neiva

unread,
Nov 23, 2009, 7:05:11 AM11/23/09
to iphonesdkd...@googlegroups.com
So you mean when string is @"" [stringItems count] returns 1, but should be returning 0?

Try something like this:

if ([[stringItems objectAtIndex:0] isEqualToString:@""]) {
[arrayCounts addObject:@"0"];
} else {
[arrayCounts addObject: [NSString stringWithFormat:@"%d", [stringItems count];
}

Cantek Batur

unread,
Nov 23, 2009, 2:51:04 PM11/23/09
to iphonesdkd...@googlegroups.com
worked perfectly.

Thanks a million.

lvcha

unread,
Nov 23, 2009, 1:48:00 AM11/23/09
to iphonesdkd...@googlegroups.com
Please provide details of the array objects type.
if you wanna separate NSStringArray object by " : ", you even don't
have to create another array.as following:

NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];

you may just do :
NSMutableArray *arrayCount = [[NSArray alloc] initWithObjects: array, ...nil];
self.itemCount = arrayCount;
[arrayCount release];

if the array above is actual not NSString object,you must declare
details of the array objects type before you create the array above.

any problem please feel free to email me.

David Phillip Oster

unread,
Nov 24, 2009, 5:26:52 PM11/24/09
to iPhone SDK Development
To graphically merge two images into a new image, you do something
like this:

UIImage *result = nil;
unsigned char *data = calloc(1,
size.width*size.height*kBytesPerPixel);
if (data != NULL) {
// kCGImageAlphaPremultipliedLast documented by Apple as the value
to use
// for a context with alpha
CGContextRef context = CGBitmapContextCreate(
data, size.width, size.height, 8, size.width*kBytesPerPixel,
CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
if (context != NULL) {
UIGraphicsPushContext(context);
// UIImage is upside down compared to CGContext.
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1, -1);
[image drawInRect:imageRect];
[image2 drawInRect:image2Rect];
UIGraphicsPopContext();
CGImageRef imageRef = CGBitmapContextCreateImage(context);
if (imageRef != NULL) {
result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
CGContextRelease(context);
}
free(data);
}
return result;

Asad Ullah

unread,
Nov 25, 2009, 6:31:50 AM11/25/09
to iphonesdkd...@googlegroups.com
The code you send works fine when you are reading static images from resources folder.
when i take image from camera then the problem is that i cannot get the final output image
in portrait form (up - down side form). i can only get the final image in landscape form (horizontal form) even i used the function correctly
CGContextTranslateCTM(context, 0, size.height);
   CGContextScaleCTM(context, 1, -1);
but still the problem appears .
please reply me.



--

You received this message because you are subscribed to the Google Groups "iPhone SDK Development" group.
To post to this group, send email to iphonesdkd...@googlegroups.com.
To unsubscribe from this group, send email to iphonesdkdevelop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphonesdkdevelopment?hl=en.



David Phillip Oster

unread,
Nov 30, 2009, 6:11:46 PM11/30/09
to iPhone SDK Development
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1, -1);

Just flips the image from upside down to right side up. To rotate the
image by 90 degrees, you need to something more like:

CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, size.width/2., size.height/2.);
CGContextRotateCTM(context, M_PI/2.);
CGContextTranslateCTM(context, -size.height/2., -size.width/2.);

That is, move the 0,0 of the coordinate system to the center of the
image, rotate is by PI/2 radians (90 degrees), then move the
coordinate system back to zero zero.

(Not tested.)
Reply all
Reply to author
Forward
0 new messages