Skip to content

Instantly share code, notes, and snippets.

@mjm918 mjm918/CreateImage.h
Created Sep 11, 2019

Embed
What would you like to do?
React Native iOS Watermark
//
// CreateImage.h
// Created by Mohammad Julfikar on 28/06/2019.
// Copyright © 2019 Facebook. All rights reserved.
//
#import "RCTBridgeModule.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CreateImage : NSObject<RCTBridgeModule>
@end
NS_ASSUME_NONNULL_END
//
// CreateImage.m
//
// Created by Mohammad Julfikar on 28/06/2019.
// Copyright © 2019 Facebook. All rights reserved.
//
#import "CreateImage.h"
@implementation CreateImage
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(imagePath:(NSString*)path watermark:(NSString*)watermark newPath:(RCTResponseSenderBlock)callback){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempImagePath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@", @"share.jpeg"]];
NSError *error = nil;
BOOL msg = [[NSFileManager defaultManager] removeItemAtPath:tempImagePath error:&error];
if(!msg){
NSLog(@"Temp watermark FAILED to delete... Path ->%@",tempImagePath);
}
NSString *tempWatermark = [NSString stringWithFormat:@"%@",watermark];
UIImage *current =[UIImage imageWithContentsOfFile:path];
int width = current.size.width;
int height = current.size.height;
if(width == 0){
return;
}
int minimalRate = 2;
int fontSize = 12;
int reserved = 100;
if(width / 1000 > 2){
minimalRate = width / 1000;
fontSize += minimalRate + 4;
}
int todoWidth = width / minimalRate;
int todoHeight = height / minimalRate;
if(height < 1000 || width < 1000){
todoWidth = width;
todoHeight = height;
}
int canvasHeight = todoHeight + reserved;
UIGraphicsBeginImageContext(CGSizeMake(todoWidth, todoHeight));
[current drawInRect:CGRectMake(0, 0, todoWidth, todoHeight)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *tcv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, todoWidth, canvasHeight)];
[tcv setBackgroundColor:[UIColor blackColor]];
CGSize gcSize = tcv.frame.size;
UIGraphicsBeginImageContext(gcSize);
[tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *blankImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContextWithOptions(blankImage.size, FALSE, 0.0);
[blankImage drawInRect:CGRectMake( 0, 0, todoWidth, canvasHeight)];
[resizedImage drawInRect:CGRectMake( 0, 0, todoWidth, todoHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIColor *fontColor = [UIColor whiteColor];
UIGraphicsBeginImageContextWithOptions(newImage.size, YES, 0.0f);
[newImage drawInRect:CGRectMake(0,0,newImage.size.width,newImage.size.height)];
CGRect rect = CGRectMake(10, todoHeight+10, newImage.size.width, newImage.size.height);
[[UIColor whiteColor] set];
UIFont *font = [UIFont boldSystemFontOfSize:fontSize];
if([watermark respondsToSelector:@selector(drawInRect:withAttributes:)])
{
NSDictionary *att = [NSDictionary dictionaryWithObjects:
@[font, fontColor]
forKeys:
@[NSFontAttributeName, NSForegroundColorAttributeName]];
[watermark drawInRect:rect withAttributes:att];
}
else
{
[watermark drawInRect:CGRectIntegral(rect) withFont:font];
}
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(newImage, 0.85f);
[imageData writeToFile:tempImagePath atomically:YES];
callback(@[tempImagePath]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.