Knowledge Base - Add image and scale it with finger (maintaining proportions)

This article shows how to add an image and scale it with finger (maintaining proportions).

In PDFView class, add the following methods:

- (BOOL)vImageStart
{
    if( ![m_doc canSave] ) return false;
    if( m_status == sta_none )
    {
        self.scrollEnabled = false;
        m_status = sta_image;
        return true;
    }
    
    return false;
}
 
-(void)vImageCancel
{
    if( m_status == sta_image )
    {
        self.scrollEnabled = true;
        m_status = sta_none;
        [self refresh];
    }
}
-(void)vImageEnd
{
    if( m_status == sta_image )
    {
        m_modified = true;
        m_status = sta_none;
 
        [self refresh];
 
        self.scrollEnabled = true;
    }
}
 
-(bool)OnImageTouchBegin:(CGPoint)point
{
    if( m_status != sta_image ) return false;
    
    imgAnnot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon57.png"]];
    
    imgAnnot.center = CGPointMake(self.contentOffset.x + point.x - (imgAnnot.frame.size.width / 2), self.contentOffset.y + point.y  - (imgAnnot.frame.size.height / 2));
    
    [self addSubview:imgAnnot];
    
    return true;
}
 
-(bool)OnImageTouchMove:(CGPoint)point
{
    if( m_status != sta_image ) return false;
    
    CGRect origin = imgAnnot.frame;
    
    float deltaMoveX = self.contentOffset.x + point.x - origin.origin.x - origin.size.width;
    float deltaMoveY = self.contentOffset.y + point.y - origin.origin.y - origin.size.height;
    float prop = imgAnnot.frame.size.width / imgAnnot.frame.size.height;
 
    if (self.contentOffset.x + point.x > origin.origin.x && self.contentOffset.y + point.y > origin.origin.y) {
        float width = (deltaMoveX > deltaMoveY) ? self.contentOffset.x + point.x - origin.origin.x : (self.contentOffset.y + point.y - origin.origin.y) * prop;
        float height = (deltaMoveX < deltaMoveY) ? self.contentOffset.y + point.y - origin.origin.y : (self.contentOffset.x + point.x - origin.origin.x) / prop;
        [imgAnnot setFrame:CGRectMake(origin.origin.x, origin.origin.y, width, height)];
    }
    
    return true;
}
 
-(bool)OnImageTouchEnd:(CGPoint)point
{
    if( m_status != sta_image ) return false;
    
    struct PDFV_POS pos1;
    struct PDFV_POS pos2;
    
    [m_view vGetPos:&pos1 :(imgAnnot.frame.origin.x - self.contentOffset.x) * m_scale :(imgAnnot.frame.origin.y - self.contentOffset.y) * m_scale];
    [m_view vGetPos:&pos2 :(imgAnnot.frame.origin.x - self.contentOffset.x + imgAnnot.frame.size.width) * m_scale :(imgAnnot.frame.origin.y - self.contentOffset.y + imgAnnot.frame.size.height) * m_scale];
    
    PDF_RECT rect;
    
    rect.left = pos1.x;
    rect.right = pos2.x;
    rect.top = pos1.y;
    rect.bottom = pos2.y;
    
    [self vAddImageWithImage:imgAnnot.image withRect:rect];
    
    [imgAnnot removeFromSuperview];
    
    return true;
}
 
- (void)vAddImageWithImage:(UIImage *)image withRect:(PDF_RECT)rect
{
    // Create the cache file
    NSString *tp = NSTemporaryDirectory();
    tp = [tp stringByAppendingPathComponent:@"cache.dat"];
    [m_doc setCache:tp];
    
    // Create the PDFPage instance of the current page
    PDFPage *page = [m_doc page:m_cur_page];
    
    // Create the CGImageRef of the image
    CGImageRef ref = [image CGImage];
    
    // Add the image
    [page addAnnotBitmap:ref :YES :&rect];
    
    // Re-render the current page
    [m_view vRenderSync:m_cur_page];
    [self refresh];
    
    // Save the PDF file
    [m_doc save];
}
 

Now you should update touchesBegan, touchesMoved, touchesEnded method and add OnImageTouchBegin:point, OnImageTouchMoved:point and OnImageTouchEnded:point method, in this way:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ...
    ...
    ...
        if( [self OnNoteTouchBegin:point] ) return;
        if( [self OnInkTouchBegin:point] ) return;
        if( [self OnRectTouchBegin:point] ) return;
        if( [self OnEllipseTouchBegin:point] ) return;
        if ([self OnImageTouchBegin:point]) return;
        [self OnNoneTouchBegin:point:touch.timestamp];
    }
}
 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    ...
    ...
    ...
        if( [self OnNoteTouchMove:point] ) return;
        if( [self OnInkTouchMove:point] ) return;
        if( [self OnRectTouchMove:point] ) return;
        if( [self OnEllipseTouchMove:point] ) return;
        if( [self OnImageTouchMove:point] ) return;
        [self OnNoneTouchMove:point:touch.timestamp];
    }
}
 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    ...
    ...
    ...
            if( [self OnNoteTouchEnd:point] ) return;
            if( [self OnInkTouchEnd:point] ) return;
            if( [self OnRectTouchEnd:point] ) return;
            if( [self OnEllipseTouchEnd:point] ) return;
            if( [self OnImageTouchEnd:point] ) return;
            [self OnNoneTouchEnd:point:touch.timestamp];
        }
    }
}
 

Now you can call [m_view vImageStart] and [m_view vImageEnd] to toggle the image insert mode.

In this example we used a static image "icon57.png", but you could edit that method and use a custom image.

Applies To

RadaeePDF SDK for iOS

Details

Created : 2015-08-07 11:49:35, Last Modified : 2015-09-24 09:11:51

Order history

Login to handle your order history.