我有一个iPhone应用程序,其中我想插入一个事件,如用户点击图像视图然后打开图像视图,从URL而不是缩放功能上传图像。我怎么能在我的代码中这样做?我在下面显示我的代码:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Tap view for background
tapView = [[UIViewTap alloc] initWithFrame:frame];
tapView.tapDelegate = self;
tapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tapView.backgroundColor = [UIColor blackColor];
[self addSubview:tapView];
// Image view
photoImageView = [[UIImageViewTap alloc] initWithFrame:CGRectZero];
photoImageView.tapDelegate = self;
photoImageView.contentMode = UIViewContentModeCenter;
photoImageView.backgroundColor = [UIColor blackColor];
[self addSubview:photoImageView];
// Spinner
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.hidesWhenStopped = YES;
spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
[self addSubview:spinner];
// Setup
self.backgroundColor = [UIColor blackColor];
self.delegate = self;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return self;}
- (void)setIndex:(NSUInteger)value {
if (value == NSNotFound) {
// Release image
photoImageView.image = nil;
} else {
// Reset for new page at index
index = value;
// Display image
[self displayImage];
}
}
//获取并显示图像
- (void)displayImage {
if (index != NSNotFound && photoImageView.image == nil) {
// Reset
self.maximumZoomScale = 1;
self.minimumZoomScale = 1;
self.zoomScale = 1;
self.contentSize = CGSizeMake(0, 0);
// Get image
UIImage *img = [self.photoBrowser imageAtIndex:index];
if (img) {
// Hide spinner
[spinner stopAnimating];
// Set image
photoImageView.image = img;
photoImageView.hidden = NO;
// Setup photo frame
CGRect photoImageViewFrame;
photoImageViewFrame.origin = CGPointZero;
photoImageViewFrame.size = img.size;
photoImageView.frame = photoImageViewFrame;
self.contentSize = photoImageViewFrame.size;
// Set zoom to minimum zoom
[self setMaxMinZoomScalesForCurrentBounds];
} else {
// Hide image view
photoImageView.hidden = YES;
[spinner startAnimating];
}
[self setNeedsLayout];
}
}
//图片失败,所以只显示黑色!
- (void)displayImageFailure {
[spinner stopAnimating];
}
- (void)setMaxMinZoomScalesForCurrentBounds {
// Reset
self.maximumZoomScale = 1;
self.minimumZoomScale = 1;
self.zoomScale = 1;
// Bail
if (photoImageView.image == nil) return;
// Sizes
CGSize boundsSize = self.bounds.size;
CGSize imageSize = photoImageView.frame.size;
// Calculate Min
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
CGFloat minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible
// If image is smaller than the screen then ensure we show it at
// min scale of 1
if (xScale > 1 && yScale > 1) {
minScale = 1.0;
}
// Calculate Max
CGFloat maxScale = 2.0; // Allow double scale
// on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
// maximum zoom scale to 0.5.
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
maxScale = maxScale / [[UIScreen mainScreen] scale];
}
// Set
self.maximumZoomScale = maxScale;
self.minimumZoomScale = minScale;
self.zoomScale = minScale;
// Reset position
photoImageView.frame = CGRectMake(0, 0, photoImageView.frame.size.width, photoImageView.frame.size.height);
[self setNeedsLayout];
}
- (void)layoutSubviews {
// Update tap view frame
tapView.frame = self.bounds;
// Spinner
if (!spinner.hidden) spinner.center = CGPointMake(floorf(self.bounds.size.width/2.0),
floorf(self.bounds.size.height/2.0));
// Super
[super layoutSubviews];
// Center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = photoImageView.frame;
// Horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = floorf((boundsSize.width - frameToCenter.size.width) / 2.0);
} else {
frameToCenter.origin.x = 0;
}
// Vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = floorf((boundsSize.height - frameToCenter.size.height) / 2.0);
} else {
frameToCenter.origin.y = 0;
}
// Center
if (!CGRectEqualToRect(photoImageView.frame, frameToCenter))
photoImageView.frame = frameToCenter;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return photoImageView;}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[photoBrowser cancelControlHiding];
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
[photoBrowser cancelControlHiding];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[photoBrowser hideControlsAfterDelay];
}
- (void)handleSingleTap:(CGPoint)touchPoint {
[photoBrowser performSelector:@selector(toggleControls) withObject:nil afterDelay:0.2];
}
- (void)handleDoubleTap:(CGPoint)touchPoint {
// Cancel any single tap handling
[NSObject cancelPreviousPerformRequestsWithTarget:photoBrowser];
// Zoom
if (self.zoomScale == self.maximumZoomScale) {
// Zoom out
[self setZoomScale:self.minimumZoomScale animated:YES];
} else {
// Zoom in
[self zoomToRect:CGRectMake(touchPoint.x, touchPoint.y, 1, 1) animated:YES];
}
// Delay controls
[photoBrowser hideControlsAfterDelay];
}
//图片视图
- (void)imageView:(UIImageView *)imageView singleTapDetected:(UITouch *)touch { [self handleSingleTap:[touch locationInView:imageView]]; }
- (void)imageView:(UIImageView *)imageView doubleTapDetected:(UITouch *)touch { [self handleDoubleTap:[touch locationInView:imageView]]; }
//背景视图
- (void)view:(UIView *)view singleTapDetected:(UITouch *)touch { [self handleSingleTap:[touch locationInView:view]]; }
- (void)view:(UIView *)view doubleTapDetected:(UITouch *)touch {
[self handleDoubleTap:[touch locationInView:view]]; }
答案 0 :(得分:0)
您可以通过这种方式完成此任务。当您向scrollView添加图像时,在imageView上添加一个透明按钮,然后从按钮的操作中获取您的数组或您存储的其他位置的特定图像的URL,然后使用HTTPRequest上传图像。