Wednesday, October 28, 2026
HomeiOS Developmentios - Goal c - Learn how to programmatically set UIImage DPI...

ios – Goal c – Learn how to programmatically set UIImage DPI of JPEG file whereas saving it’s altering after saved in library

[ad_1]

I have to convert UIImage to particular DPI for instance “300 DPI”,I am utilizing beneath code to alter picture metadata and changing to 300 DPI,Metadata is altering however after saving,I’ve checked picture properties picture is exhibiting “72 DPI”,how am i able to get picture in 300 DPI

-(NSData *)changeMetaDataInImage
{
NSData *sourceImageData = [[NSData alloc] initWithContentsOfFile:@”~/Desktop/1.jpg”];
if (sourceImageData != nil)
{
CGImageSourceRef supply = CGImageSourceCreateWithData((__bridge CFDataRef)sourceImageData, NULL);

    NSDictionary *metadata = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(supply, 0, NULL);

    NSMutableDictionary *tempMetadata = [metadata mutableCopy];
    [tempMetadata setObject:[NSNumber numberWithInt:300] forKey:@"DPIHeight"];
    [tempMetadata setObject:[NSNumber numberWithInt:300] forKey:@"DPIWidth"];

    NSMutableDictionary *EXIFDictionary = [[tempMetadata objectForKey:(NSString *)kCGImagePropertyTIFFDictionary] mutableCopy];
    [EXIFDictionary setObject:[NSNumber numberWithInt:300] forKey:(NSString *)kCGImagePropertyTIFFXResolution];
    [EXIFDictionary setObject:[NSNumber numberWithInt:300] forKey:(NSString *)kCGImagePropertyTIFFYResolution];

    NSMutableDictionary *JFIFDictionary = [[NSMutableDictionary alloc] init];
    [JFIFDictionary setObject:[NSNumber numberWithInt:300] forKey:(NSString *)kCGImagePropertyJFIFXDensity];
    [JFIFDictionary setObject:[NSNumber numberWithInt:300] forKey:(NSString *)kCGImagePropertyJFIFYDensity];
    [JFIFDictionary setObject:@"1" forKey:(NSString *)kCGImagePropertyJFIFVersion];

    [tempMetadata setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyTIFFDictionary];
    [tempMetadata setObject:JFIFDictionary forKey:(NSString *)kCGImagePropertyJFIFDictionary];

    NSMutableData *destinationImageData = [NSMutableData data];

    CFStringRef UTI = CGImageSourceGetType(supply);

    CGImageDestinationRef vacation spot = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)destinationImageData, UTI, 1, NULL);

    CGImageDestinationAddImageFromSource(vacation spot, supply,0, (__bridge CFDictionaryRef) tempMetadata);

    CGImageDestinationFinalize(vacation spot);

    return destinationImageData;
}

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments