I have taken screenshot programmatically for a scroll view in xcode. But the problem i am facing is, i need to take screenshot along with present date in it. Please help me.
Before taking screenshot, put one label with current date, and remove it after taking screenshot.
Or you could use the EXIF meta data to record the date (it's probably already in there). I guess an embedded date in the image is harder to fake, though.
This is how you can add a text to an image. Try this with the date as string. Once you taken the screenshot pass it to the below method and use the output image. Modify the below method for your convenience.
//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
Source
Related
I'm trying to fill a CGBitmapContext with a solid red color and get CGImgRef, but image is all transparent.
Here's the code
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
1,
1,
8,
0,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGFloat components[] = {1.0,0.0,0.0,1.0};
CGContextSetFillColor(context, components);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGImageRef imgRef = CGBitmapContextCreateImage(context);
Please don't recommend using UIBezierPaths and other UIKit things. I need to use CoreGraphics.
The problem is that you forgot to set a fill color space. You must do that in order for CGContextSetFillColor to work correctly (because that tells it how many components to expect and how to interpret them). This works:
CGFloat components[] = {1,0,0,1};
CGContextSetFillColorSpace(context, colorSpace); // <-- this was missing
CGContextSetFillColor(context, components);
// and now fill ...
problem was solved using CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); Btw, it would be interesting to know what i was doing wrong. Also Apple documentation on CGContextSetFillColor says "Note that the preferred API to use is now CGContextSetFillColorWithColor."
I wanna to build a VNC client project in iPad,but when i'm using NPDeskTop demo ,i found that when the picture changes, the screen on iPad maybe show some black lines. I want to know how to fix it.
Here is the demo adress: enter link description here
int bytesPerPixel = (pixelFormat.bitsPerPixel + 7) >> 3;
CGContextTranslateCTM(ctx, 0, _size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
// FIXME: rect origin can be negative?
uint32_t *start = (uint32_t *)_buffer.bytes;
int bytesPerRow = _size.width * bytesPerPixel;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextRef bmpctx = CGBitmapContextCreate(start, _size.width, _size.height,8 , bytesPerRow, cs, kCGImageAlphaNoneSkipFirst);
CGImageRef image = CGBitmapContextCreateImage(bmpctx);
CGContextDrawImage(ctx, rect, image);
CGImageRelease(image);
CGColorSpaceRelease(cs);
CGContextRelease(bmpctx);
here is my create view func.
I have created a Google map view where I am setting markers on their respective positions and and adding some text over them. Now I want to change size of that text as per zoom, means that size should be bigger on zoom out and lesser on map zoom in. How to achieve the same ? I tried different logic but failed. I am creating that marker text using following function,
// Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1 red:(int)red green:(int)green blue:(int)blue alpha:(float)Alpha{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = (CGBitmapInfo) kCGImageAlphaPremultipliedFirst;
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, bitmapInfo);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
char * text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
//Here I set size of text
CGContextSelectFont(context, "HelveticaNeue-Bold", 15, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextDrawingMode(context, kCGTextInvisible);
CGContextShowTextAtPoint(context, 0, 0, text, strlen(text));
// Then get the position of the text and set the mode back to visible:
CGPoint pt = CGContextGetTextPosition(context);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1); // black - for shadow
CGContextShowTextAtPoint(context, (w/2) - (pt.x / 2) + 1, h/2, text, strlen(text));
CGContextSetRGBFillColor(context, red, green, blue, Alpha); // white - for text
CGContextShowTextAtPoint(context, (w/2) - (pt.x / 2), h/2, text, strlen(text));
CGImageRef imageMasked=nil;
imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
} // addText
Is it possible to add a text string to an image when saving to camera roll?
for example photo is taken and saved to camera roll:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil,#selector(image:didFinishSavingWithError:contextInfo:), nil);
//something here to add #"my pre defined text"; to saved image
}
I want to automatically add pre defined text to this saved image without any user input if possible?
Nothing comes up on the usual searches on SO and Google regarding what I am trying to do
Take a Look on my code:
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 2);
CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
And improve with:
- (IBAction)drawImage:(id)sender {
NSString *text1 = myTextField.text;
UIImage *updatedImg = [self addText:myImageView.image text:text1];
[myImageView setImage: updatedImg];
}
I hope this help you ;)
thx for vote ;)
This question appears to address your needs:
Add Text to UIImage
Perhaps ignore the camera action, and the camera roll, what you really just want to do is add a string to a UIImage.
Need to take photo from the application with date and time the photo has been taken to be displayed in photo just like digital camera.
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1;
{
int w = img.size.width;
int h = img.size.height;
//lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 1);
//rotate text
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}