0
votes

I have a UIImageView inside a UIScrollView with zooming enabled in the scrollView. What I want is if a user has zoomed into a particular region of an image , and then comes back , the scroll view sets the image wherever it was left. I have tried using content offset but it's not working for all zoom levels. In the case of zoom level 1 the imageView kind of sticks to the left of the scrollView . This is the code that I have used to save the contentOffset and zoomScale .

    -(void)saveContentOffset
{
    NSInteger zoomValue=[Scroll zoomScale];
    CGPoint offeset=[Scroll contentOffset];
    NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
    [dict setObject:[NSNumber numberWithInteger:offeset.x] forKey:@"X"];
    [dict setObject:[NSNumber numberWithInteger:offeset.y] forKey:@"Y"];
    [dict setObject:[NSNumber numberWithInteger:zoomValue] forKey:@"Zoom"];
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"Dict"];
}

and this code is used to set the contentOffset when the user comes back on the screen:

-(void)setContentOffsetForScroll
{
    NSDictionary *dict=[[NSUserDefaults standardUserDefaults] objectForKey:@"Dict"];
    if ([dict allKeys]>0)
    {
        NSInteger xPoint=[[dict valueForKey:@"X"] integerValue];
        NSInteger yPoint=[[dict valueForKey:@"Y"] integerValue];
        NSInteger zoomScale=[[dict valueForKey:@"Zoom"] integerValue];
        CGPoint offsetPoint=CGPointMake(xPoint, yPoint);
        [Scroll setZoomScale:zoomScale];
        [Scroll setContentOffset:offsetPoint];
    }
}

Please check the attached image for the issue in the case of zoomLevel 1 , the view at background with greenColor is ScrollView . In this case the content sets to left .

Please suggest what exactly I am missing.

Thanks!enter image description here

3
What's the result of your code?Gary Lyn

3 Answers

0
votes

You can use this

Save your image view's origin by x=imageview.frame.origin.x and y=imageview.frame.origin.y

Then when you come back on the same view

use this CGRect *rect=imageview.frame; rect.origin.x=x;//from previously stored value rect.origin.y=y;//from previously stored value imageview.frame=rect;

0
votes

Don't save separate X, Y and Zoom parameters. Instead, save UIScrollView.bounds and subsequently pass it to -[UIScrollView zoomToRect:animated:].

P.S. As a matter of coding style, to make your code readable to existing Objective-C programmers, don't use capital letters for variable names; they are used for class names.

0
votes

after a lot of research , i found the error that i was doing. I was using integer value for zoom scale , but to store precision data and exact value we need to use float value. So the changes are as below:

    -(void)saveContentOffset
{
    CGFloat zoomValue=[Scroll zoomScale];
    CGPoint offeset=[Scroll contentOffset];
    NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
    [dict setObject:[NSNumber numberWithInteger:offeset.x] forKey:@"X"];
    [dict setObject:[NSNumber numberWithInteger:offeset.y] forKey:@"Y"];
    [dict setObject:[NSNumber numberWithfloat:zoomValue] forKey:@"Zoom"];
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"Dict"];
}


-(void)setContentOffsetForScroll
{
    NSDictionary *dict=[[NSUserDefaults standardUserDefaults] objectForKey:@"Dict"];
    if ([dict allKeys]>0)
    {
        NSInteger xPoint=[[dict valueForKey:@"X"] integerValue];
        NSInteger yPoint=[[dict valueForKey:@"Y"] integerValue];
        NSInteger zoomScale=[[dict valueForKey:@"Zoom"] floatValue];
        CGPoint offsetPoint=CGPointMake(xPoint, yPoint);
        [Scroll setZoomScale:zoomScale];
        [Scroll setContentOffset:offsetPoint];
    }
}