Search code examples
iphoneiosxcodeipadipod

UIImage to BitmapContext


I want to convert a simple UIImage into BitmapContext, but I cannot find any tutorial to convert UIImage into BitmapContext. Kindly tell me how to do it?

Thanks!


Solution

  • May be this code help you

    CGSize newSize = CGSizeMake(320, 400);
    UIGraphicsBeginImageContext( newSize );
    
    [imageView.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    NSData * data = UIImagePNGRepresentation(newImage);
    [data writeToFile:@"foo.png" atomically:YES];
    
    
     UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);///save image in phone
    
    OR For Bitmap.
    
    CGContextRelease(context);
    context = CGBitmapContextCreate (NULL,                                                     
                                    image.size.width,
                                    image.size.height,
                                    8,    // bits per component
                                    bitmapBytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaLast
                                    );
    CGColorSpaceRelease( colorSpace );                                                                      
    
    CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
    pixelData = CGBitmapContextGetData(context);