0

I can't solve this problem. Load image, convert to CGImageRef. Try to get bitmap context and render on the screen.

 NSURL *imageFileURL = [NSURL fileURLWithPath:stringIMG];

   CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
   CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);

    NSInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
    NSInteger bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    NSInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
    NSInteger width = CGImageGetWidth(imageRef);
    NSInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorspace = CGImageGetColorSpace(imageRef);

    size_t rawData = bytesPerRow*height;
    unsigned char *data = malloc(rawData);
    memset(data2, 0, rawData);

     CGContextRef context = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorspace, bitmapInfo);


    CGImageRef imageRef2 = CGBitmapContextCreateImage(context);

   // CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef2);
    UIImage *result = [UIImage imageWithCGImage:imageRef2];//if i do like this i got white empty screen

    UIImageView *image = [[UIImageView alloc] initWithImage:result];
    [self.view addSubview:image];// if i do like this i got black rectangle on the white screen

I have no idea. I check by breakpoint that context is not null. I don't know what should i do. Please maybe anyone can help me?

user2032083
  • 313
  • 2
  • 4
  • 14

1 Answers1

0

Category added it to UIImage for resizing using CoreGraphics

UIImage+Resize.h

#import <UIKit/UIKit.h>

@interface UIImage (Resizing)

-(UIImage*)resizedImageWithSize:(CGSize)size;

@end

UIImage+Resizing.m

#import "UIImage+Resizing.h"

@implementation UIImage (Resizing)

-(UIImage*)resizedImageWithSize:(CGSize)size {


    CGImageRef cgImage = [self CGImage];

    size_t bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
    size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgImage);

    CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);

    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), cgImage);

    CGImageRef resizedImageRef = CGBitmapContextCreateImage(context);

    UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef];

    CFRelease(resizedImageRef);
    CFRelease(context);

    return resizedImage;

}

@end
Mohamed Elkassas
  • 829
  • 1
  • 13
  • 33