मैंने एक कस्टम छवि दर्शक को बहुत पहले चुटकी पहचानकर्ताओं के बिना नहीं किया था। UIScrollView के शीर्ष पर बस UIImageView देखें। वहां आप छवि के लिंक के साथ एक स्ट्रिंग पास करते हैं और इसमें प्रगति पट्टी भी होती है। एक बार जब छवि लोड हो जाती है तो छवि दिखाया जाता है।
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.theImageView;
}
- (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2;
}
else {
frameToCenter.origin.x = 0;
}
// center vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2;
}
else {
frameToCenter.origin.y = 0;
}
return frameToCenter;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
self.theImageView.frame = [self centeredFrameForScrollView:self.theScrollView andUIView:self.theImageView];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.resourceData setLength:0];
self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.resourceData appendData:data];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
self.progressBar.progress = [resourceLength floatValue]/[self.filesize floatValue];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.theImage = [[UIImage alloc]initWithData:resourceData];
self.theImageView.frame = CGRectMake(0, 0, self.theImage.size.width, self.theImage.size.height);
self.theImageView.image = self.theImage;
self.theScrollView.minimumZoomScale = self.theScrollView.frame.size.width/self.theImageView.frame.size.width;
self.theScrollView.maximumZoomScale = 2.0;
[self.theScrollView setZoomScale:self.theScrollView.minimumZoomScale];
self.theScrollView.contentSize = self.theImageView.frame.size;
self.theLabel.hidden = YES;
self.progressBar.hidden = YES;
}
-(void)setImageInImageView
{
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.imageLink]];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:req delegate:self];
if (conn)
{
self.resourceData = [NSMutableData data];
}
else
{
NSLog(@"Connection failed: IMageViewerViewController");
}
}
-(void)loadView
{
self.filesize = [[NSNumber alloc]init];
self.progressBar = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
self.progressBar.frame = CGRectMake(20, 240, 280, 40);
[self.progressBar setProgress:0.0];
self.theImageView = [[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
self.theScrollView = [[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
self.theScrollView.delegate = self;
[self.theScrollView addSubview:self.theImageView];
self.view = self.theScrollView;
self.theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 320, 40)];
self.theLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.theLabel.text = @"Please wait, file is being downloaded";
self.theLabel.textAlignment = UITextAlignmentCenter;
self.theLabel.hidden = NO;
[self.view addSubview:self.progressBar];
[self.view bringSubviewToFront:self.progressBar];
[self.view addSubview:self.theLabel];
[self.view bringSubviewToFront:self. theLabel];
[self performSelectorOnMainThread:@selector(setImageInImageView) withObject:nil waitUntilDone:NO];
}
और हेडर फाइल: कोड यह
@interface ImageViewerViewController : UIViewController<UIScrollViewDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
@property (nonatomic, retain) IBOutlet UIScrollView *theScrollView;
@property (nonatomic, retain) NSString *imageLink;
@property (nonatomic, retain) UIImage *theImage;
@property (nonatomic, retain) UILabel *theLabel;
@property (nonatomic, retain) UIProgressView *progressBar;
@property (nonatomic, retain) NSMutableData *resourceData;
@property (nonatomic, retain) NSNumber *filesize;
@end
आशा है कि यह
हाँ, मैंने वह सब किया लेकिन प्रतिनिधि को – adit
आह कहा नहीं जाता है! मैंने maxZoomScale सेट नहीं किया और इसलिए यह काम नहीं किया! धन्यवाद – adit
इग्नासिओ कृपया आप छवि दृश्य और scrollView पर बाधाओं को बता सकते हैं। मैंने वही किया लेकिन यह काम नहीं कर रहा है। मेरा छवि दृश्य पहलू भरने के लिए सेट है और चौड़ाई छवि से बाहर जा रही है फ्रेम देखें इसलिए मुझे छवि दृश्य फ्रेम बढ़ाने के साथ-साथ सामग्री आकार चौड़ाई को स्क्रॉल करने की आवश्यकता है। मेरे स्क्रॉल व्यू में मेरे पास केवल एक छवि है –