[iPhone] UIImageView in UIScrollView

Ok, this is something that's been hanging in my code for a while and I just can't get it to work. I just need my UIImageView to get centered in a UIScrollView and zoom working properly. The Problem is the centering, either you set the Frame of your UIImageView to the size of your UIScrollView and the frame being too large the edge of the image bounces off the image, or you set the UIImageView to the frame of the enclosing UIImage but then the UIImageView is placed on the top of the UIScrollView's coordinates. I've tried with .center .contentSize .contentOffSet/inset, the most promizing beeing UIImageView.center = UIScrollView.center. The problem is after the zoom the height of the contentSize of the UIScrollView is plain wrong, exceeding the new size of the UIImageView. I've set an xcode project covering this topic at http://public.me.com/michaf
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIImage* image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Vespa" ofType:@"jpg"]];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor blackColor];
window.backgroundColor = [UIColor blackColor];
// I'm setting the UIImageView size so it fills the screen first
float ratio = image.size.width/[[UIScreen mainScreen] bounds].size.width;
CGRect imageFrame = CGRectMake(0,0, [[UIScreen mainScreen] bounds].size.width, (image.size.height/ratio));
imageView.frame = imageFrame;
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
//imageView.center = scrollView.center; This is one of my many attempts, try this, the actual size of the scrollView's content view after the zoom gets a wrong height
[image release];
[imageView release];
[window addSubview:scrollView];
[window makeKeyAndVisible];
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;

Adding these code seems help, but I don't know which is the right place to put these codes.
scrollView.frame = self.view.bounds;
scrollView.contentSize = self.imageview.bounds.size;
[scrollView setNeedsDisplay];
BR,
Edward Yang

Similar Messages

  • Event handling for UIImageView inside UIScrollView

    Hi, I have a UIScrollVIew. I added a UIImageView inside it. I found that UIImageView did not reveive any events. No matter what I do, there is only image zoom in out and flip. How can I enable UIImageVew to receive some events?
    Thanks,
    fm

    Did you set userInteractionEnabled per my reply to the question you posted earlier today? [http://discussions.apple.com/thread.jspa?messageID=9043590#9043590]

  • (iPhone SDK 7) UIScrollView subview touch problem

    New here, in fact new to the SDK but getting on well with it (plenty of previous coding experience to draw on). However, I've been hitting my head against a brick wall with UIScrollView and it making subviews, e.g. UIImageViews, no longer be touchable. When I move the image views outside the scroll view it's fine, so I know they're working and the touchesBegan:withEvent is processing as it should do.
    Here's what I'm doing in the initWithFrame:
    scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,400)];
    scroller.scrollEnabled = YES;
    CGSize sz;
    sz.width = 320;
    sz.height = 800;
    scroller.contentSize = sz;
    scroller.tag = 400;
    scroller.userInteractionEnabled = YES;
    scroller.delaysContentTouches = YES;
    scroller.canCancelContentTouches = NO;
    [self addSubview:scroller];
    (I've tried all different combinations of delaysContentTouches and canCancelContentTouches just to see if they made a difference, originally I hadn't got either in there. But they don't seem to alter anything)
    And then the image views:
    buttonPlay = [[UIImageView alloc] initWithFrame:CGRectMake(20,320,40,40)];
    buttonPlay.image = [UIImage imageNamed:@"blah.jpg"];
    buttonPlay.userInteractionEnabled = YES;
    buttonPlay.tag = 100;
    [scroller addSubview:buttonPlay];
    buttonSettings = [[UIImageView alloc] initWithFrame:CGRectMake(80,320,40,40)];
    buttonSettings.image = [UIImage imageNamed:@"blah.jpg"];
    buttonSettings.userInteractionEnabled = YES;
    buttonSettings.tag = 101;
    [scroller addSubview:buttonSettings];
    Basically no touch events are being generated anywhere I click within the scroll view and I'm at a loss to work out why.
    Not sure if it's related either but I can't get a UILabel to respond to a touch either, even though I've enabled userInteractionEnabled for it - even trying a simple project I can't get a UILabel to generate a touch event at all.
    Any help on the above scroll view issue would be appreciated as it's driving me nuts!
    Cheers

    UIScrollView must not pass these events up the responder chain. I'm not sure if this is intended behavior, but it seems like it might be (if so the docs should be updated). I took my test code and verified:
    1. scroller's nextResponder points the UIView of the ViewController.
    2. That UIView has a nextResponder pointing to the ViewController
    This is what you would expect from the docs on the UIResponder Chain. However, it relies on each UIView subclass to pass the event up the responder chain +if it isn't handled fully+. UIScrollView must be making the assumption that "the buck stops here". Hence your touchesBegan: in your viewController never gets the message.
    Further, a little more testing with this at the end of viewDidLoad:
    BOOL status1 = [scroller canResignFirstResponder];
    BOOL status2 = [scroller resignFirstResponder];
    BOOL status3 = [self.view becomeFirstResponder];
    showed me:
    status1 = NO
    status2 = NO
    status3 = NO
    I'd say UIScrollView likes the power it wields .

  • View Doc (pdf, image, .doc) imported as BLOB files in UIImageView within a UIScrollView.

    Hi,
    I am new to IOS programming and after researching on topic i found post saying it is possible to view BLOB file in uiimageView.
    I have already done sucessfully for BLOB file of Image. but i can not view pdf file, even if i got NSData.
    /****** from SP ****/
    Insert TestBlob(tbName, tbDesc, tbBin) Select
    'Test.pdf','PDF file', BulkColumn from Openrowset( Bulk
    'C:\blob\udoc\81.pdf', Single_Blob) as tb
    /****** from my App *******/
    NSString *strImage; // String has BLOB file from DB
    NSData *imageData = [self dataFromBase64EncodedString:strImage];
    UIImage *image = [UIImage imageWithData:imageData];
    UIImageView *m_oImageViewer = [[UIImageView alloc]init];
    m_oImageViewer.image = image;
    m_oImageViewer.frame=CGRectMake((self.view.frame.size.width/2)- 250,60,500,500);
    [self.view addSubview:m_oImageViewer];
    -(NSData *)dataFromBase64EncodedString:(NSString *)string{
    if (string.length > 0)
         NSString *data64URLString = [NSString stringWithFormat:@"data:;base64,%@", string];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data64URLString]];
        return data;
    return nil;
    Please help me. i have already viewd Image but PDF comming empty. i do not want to edit or do anything only user should be able to view uploaded Doc from IPD App. and Doc can be image, pdf or .doc or excel sheet. i get BLOB file for all file types which i pass to my App.
    IS what i am doing is correct? as i imagine BLOB is common for all file Types. still i can not see pdf file(i have not tried other file formate only .png and pdf).
    Please guid me if i am missing anything.

    Hi Sreelatha. The best example code for your purpose is the Scrolling sample app I referred to previously. The word Scrolling in that post is a link to the download page. I think that code will answer all your questions about how to init a UIImage, then use it to init a UIImageView, and then position the UIImageView frame in a UIScrollView. Two cases are demostrated: 1) Scrolling horizontally through a series of images; 2) Scrolling both H & V over a single image which is several times larger than the window.
    You might also be interested in the PageControl sample app in case you want to add a page control to your scroll view.
    Reading your last post again... I sure hope we've been discussing images (like jpegs?) in this thread. This isn't about reading and rendering the actual xls, pdf and doc files, is it?? Of course the iPhone handles html beautifully, so for example, an html representation of a doc file would play nicely. But in that case you'd want a UIWebView, not a UIImageView.

  • [iPhone] Pinch/zoom in UIScrollView at simulator does not work

    Hi everyone,
    I tried to get pinch/zoom working for a UIImageView within a UIScrollView. Therefore I downloaded the source code from the tutorial under http://www.vimeo.com/1642150. When I started the app in the iPhone simulator I used the options key and the mouse pad to simulate zoom/pinch gesture, but nothing happend (the two gray circles are shown in the simulator which indicates that zoom/pinch gesture is used for input). The zoom/pinch does not work within the UIScrollView, whatever I try, nothing worked. None of the UIScrollView delegate methods are called (used NSLog to check it). Did anybody has a similar problem or behaviour?
    Thanks for help.

    Thanks for the reply. I finally found the problem. You have to use two fingers and the options key to simulate zoom/pinch gestures with a trackpad, although the two gray circles are shown when use only one finger, zoom/pinch only works if you use two finger, like you would do for scrolling.

  • How to display.xls, pdf, .doc files in UIImageView within a UIScrollView.

    Hi,
    I am new to iPhone programming. I need some help.
    I want to display .xls, pdf, .doc files in UIImageView within a UIScrollView programtically how can i do this. What is the procedure.
    Pls. provide me a sample application.
    If anyone knows pls. respond it would be of great help for me.
    Thanks in Advance,
    Regards,
    Sreelatha.

    Hi Sreelatha. The best example code for your purpose is the Scrolling sample app I referred to previously. The word Scrolling in that post is a link to the download page. I think that code will answer all your questions about how to init a UIImage, then use it to init a UIImageView, and then position the UIImageView frame in a UIScrollView. Two cases are demostrated: 1) Scrolling horizontally through a series of images; 2) Scrolling both H & V over a single image which is several times larger than the window.
    You might also be interested in the PageControl sample app in case you want to add a page control to your scroll view.
    Reading your last post again... I sure hope we've been discussing images (like jpegs?) in this thread. This isn't about reading and rendering the actual xls, pdf and doc files, is it?? Of course the iPhone handles html beautifully, so for example, an html representation of a doc file would play nicely. But in that case you'd want a UIWebView, not a UIImageView.

  • IPhone SDK: UIScrollView, zooming works on simulator but not device

    I've got a UIScrollView set up to zoom an image. It worked fine under Beta 5, both on an iPod Touch and in the simulator.
    In Beta 6, after changing the method scrollViewWillBeginZooming to viewForZoomingInScrollView the zoom works fine in the simulator, but it does not work on the iPod.
    Any ideas?

    Excited to hear that you were able to zoom image.
    I am ****** off, sure because I am new in this.
    I got one UIImageView in UIView.
    And I can display Images.
    I like to implement the pinch and zoom function.
    From my googling I understood that UImageView should be inside a UIScrollView
    But I dont have any idea how to do that.
    Can you please send me the sample code for doing the same.
    Thanks in advance.
    If you guys no some books to refer , please inform me.
    Expecting your help

  • [iPhone] UIScrollView responder chain problems

    I have a view hierarchy something with a UIImageView as a subview of a UIScrollView as a subview of my view controller's view.
    According to the documentation, the each object in the responder chain is supposed to pass a touch event to it's view controller (if it has one, and then onto it's superview). So, if I tap the uiimage, the touch event should eventually get passed up to the top level view controller where I can handle it.
    Well, it's not working....the event never reaches the top level. Only touches on direct subviews of the top view are received by the controller.
    Does this work for anyone else, with a similar hierarchy ? Are there any good techniques or tools for debugging the responder chain ?

    Hello -
    I faced a similar issue.
    I used the following to force my UIView to report to its parent UIScrollView:
    [self.nextResponder.nextResponder touchesBegan:touches withEvent:event];
    I am not exactly sure why but I had to jump one "nextResponder" to go directly to the one above.
    This is all quite obscure to me; but somehow it seems to work....
    BTW, it is possible to check "who is who" using:
    NSLog(@"%s",[self.nextResponder.description cStringWithEncoding:1]);
    (self-made probably-quite-primitive trick...)
    Hope that helps.
    S

  • UIScrollView and CATiledLayer on iPhone

    I am trying to display a 350x2400 graph in a UIScrollView. The view was very choppy and this wasn't working, so I made the layer of the view in the UIScrollView a CATiledLayer, and this worked. I don't like how the tiles fade in though. Even if I subclass CATiledLayer and set the fadeDuration to 0, there is still a slight pause while scrolling before the next tile is drawn.
    Is it possible to tell the CATiledLayer object to draw the tiles adjacent to the necessary tile as well, or is there some other way to make the scrolling seem seamless, so that the next tile is already drawn before it is scrolled to?
    Thanks,
    -Mike

    Thanks good to know. As long as it's a known issue. I guess I just have to deal with it. I know that I have seen on blogs for new application updates and I would be like ok good. But it would never ever show up in my iTune or appstore on the iPhone. So I guess I just have to keep that in mind and download manually.
    Thanks again!

  • [iPhone SDK] Faster scrolling in UIScrollView?

    Hi,
    I've got paging enabled on my UIScrollView and everything works fine. However, page swiping is not very 'snappy'. I essentially want to be able to flick the page quickly like the weather app even if the page was flicked slowly. On landscape, if you swipe slowly, it decelerates and stops by the time the page has reached 75 % of the screen. After that, if you swipe quickly, it'll snap to it's page width limit.
    How can I achieve faster page snaps? Thanks!

    anyone please?

  • IPhone UIViewContentModeRedraw & UIScrollView zooming is pixellated

    Dear Experts,
    I have a UIView that draws its content using Quartz2D operations (CGContextAddLineToPoint() etc) inside its drawRect: method. This view is a subview of a scrollview. Everything is fine until I zoom. It seems that the content is magnified as an image, so it looks pixellated. I thought that the solution to this would be to set the view's contentMode to UIViewContentModeRedraw, but this doesn't seem to have helped: my debugging suggests that drawRect is not being called when the UIScrollView changes the magnification.
    I think that manual changes to the view's transform property have the same effect, i.e. they also don't cause drawRect to be invoked and the result looks pixelated.
    In test code I can get decent results by instead doing the magnification on the graphics context inside drawRect: and calling setNeedsDisplay to ensure that drawRect is called. But I don't think I can wire this hack up to the UIScrollView.
    Does anyone else have experience of zooming Quartz2D content?
    Thanks for any suggestions -- Phil.

    fwiw, I'm seeing the same issue when animating UIView frame resizes. My backing CGPathRef with a 1 pixel stroke border looks stretched during the animation.

  • IPhone SDK: UIScrollView not scrollable as a UINavigationBar title view?

    I have a custom UIView that has a UILabel in a UIScrollView. This view works fine under normal circumstances.
    When I set an instance of this view as the navigationItem.customTitleView of UIViewController, the view appears as the title on the navigation bar, but I can no longer scroll the view.
    Any ideas? Are there restrictions on navigation bar custom views that were not described in the docs? I don't recall reading any limitations.

    bump

  • Iphone sdk custom pdf viewer in uiscrollview

    just like in the subject, I am struggling to get pdf view in uiscrollview.
    now, first of all - I know I can do it with UIWebView. I am doing so now, but next version of my app - I need to gain much more control, hence need for doing it like that.
    I hope one day, apple will create example of quartz vector graphics inside uiscrollview.
    now, to the point.
    basicaly got two methods:
    - (void)renderZoomToBitmap
    int w,h;
    NSDate *start;
    NSTimeInterval elapsed;
    start = [NSDate date];
    CGPDFPageRef pageref = CGPDFDocumentGetPage(pdf, _page);
    CGRect mediaBox = CGPDFPageGetBoxRect(pageref, kCGPDFMediaBox);
    mediaBox.size.width *= zoomf;
    mediaBox.size.height *= zoomf;
    w = mediaBox.size.width+0.5;
    h = mediaBox.size.height+0.5;
    CGRect viewBounds=CGRectMake(0,0,w,h);
    char *renderBuff = malloc( w * h * 4 );
    CGContextRef cgContext = CGBitmapContextCreate(renderBuff, w, h, 8, w * 4, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrder32Host | kCGImageAlphaNoneSkipLast);
    CGContextSetRGBFillColor(cgContext, 0.1, 0.1, 0.1, 1);
    CGContextFillRect( cgContext, viewBounds );
    CGContextDrawPDFPage(cgContext, pageref);
    CGContextFlush( cgContext );
    renderPage = CGBitmapContextCreateImage(cgContext);
    free(renderBuff);
    CGContextRelease(cgContext);
    elapsed = [start timeIntervalSinceNow] * -1.0;
    // NSLog(@"view bounds past zoom w:%f h:%f", b.size.width, b.size.height);
    self.frame = viewBounds;
    NSLog(@" renderZoomToBitmap zoom:%f took %f s", zoomf, elapsed );
    and one that draws actual bitmap
    - (void)drawRect:(CGRect)rect
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSLog(@"draw rect %f %f %f %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, -_offset.x, _offset.y);
    CGContextDrawImage(context, rect, renderPage);
    [self setNeedsDisplay];
    now everytime
    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
    is called , I set new zoom scale (zoomf) in above UIView instance, and call first method - to refresh bitmap.
    it does work okay when zoomf=1.0, but when user starts zooming in/out, it does funky things.
    What is the problem , do any one of you fine folks know ? please help me out with this. I am very new to quartz.
    thanks.
    Message was edited by: gryzman

    anyone please?

  • [iPhone SDK] UIScrollView to look like in Safari

    UIScrollView seems to be setup to page through views that have pages the same size of the UIScrollView. But in Safari, the pages are about a half the size of the screen width. With a quarter of the screen margins on the left and right. This allows it to show a portion of the pages to the left and right of the current page. How can I set this up?

    Anybody?

  • [iPhone] Making a UIWebView scrollable from within a UIScrollView

    I'm building an app which consists of a UIScrollView which horizontally scrolls (much like the PageControl sample app), and which contains a UIWebView within each horizontally scrollable pane. Because of the fact that I'm using both of these objects, I'm unable to scroll or magnify the UIWebView.
    I have read on the net that there is some sort of interaction between the event handlers of the UIScrollView and the UIWebView when they co-exist which prevents the events from getting to the UIWebView, and I presume that this is what I'm seeing in my app.
    Do any of you know of any techniques or tricks which will allow my UIWebView to be scrollable and magnifiable, even when it lives within a UIScrollView?
    Thanks in advance.

    You can't. Final Cut Express was not designed to edit highly compressed formats.
    Use Mpeg Streamclip to convert the h.264 formated clips to AIC (apple intermediate codec) and edit away.
    Yes, the material may take a hit, but it will be small and, given the quality of the lens on the phone, probably not all that noticable.
    Or, purchase Final Cut X or Premiere Pro. They will work with h.264 format clips without the need to transcode to an all I frame format.
    Good luck,
    x

Maybe you are looking for

  • What HDD will work for a MacBook Pro 13" 7.1 Unibody HDD Upgrade?

    I am looking at upgrading my HDD and have been reading up on the HDD heights (9mm, 12.5mm etc). Which HDDs will work for my upgrade? I would like to get either a 750GB or 1TB drive. Will the WD Scorpio Blue drives work? I currently have the Hitachi 2

  • Need Help Upgrading OATS Ver:8.01.0249 to OATS Ver:8.50.0260

    I'm currently having troubles upgrading Oracle Application Testing Suite from version 8.01.0249 to version 8.50.0260. The database for OATS is SQL Server 2000. I've unistalled the application ver 8.01.0249 completely. Followed by installing the appli

  • Slow wireless on iPad2 after installing iOS6?

    Ever since installing iOS6 on my iPad2 I am experiencing a slow wireless connect time. Both my wife and I have an iPad2 and have both seen the connect time slow down. Pinterest is one of my wife's most used apps and it is really slow. I have read tha

  • Changing Text Headings in Web Report

    Hi SDN, Can you please assist us in this scenario, as i have not worked with Table Interface Class previously, and if you can help out with the steps it would be much appreciated. We have created a query which uses 2 structures in the Rows area.  To

  • Plugin for WMF type files

    Hello: Does anyone know if there are plugins for the vector type WMF files that can make it possible to load these in PE4? Other raster graphics applications are able to load wmf files. Why not PE4? WinXP Tony