關於此問題,我在這裏經歷了相當多的線索,並且沒有取得太大的成功。以編程方式將圖像保存到相機膠捲
我正在瀏覽網站上的圖片,我點擊並按住顯示我的操作表,保存照片或取消。它確實可以保存,但它保存了白色圖像,而不是圖像本身。我試過的另一種方法只保存爲屏幕截圖(而不是我之後的)。
我不是在一個特定的圖像後,任何形式的任何圖像。
這是我正在使用的所有當前代碼。
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
webView.userInteractionEnabled = YES;
gestureRecognizer.minimumPressDuration = 0.3;
gestureRecognizer.delegate = self;
gestureRecognizer.numberOfTouchesRequired = 1;
[webView addGestureRecognizer:gestureRecognizer];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
//get the image view that the user selected and save it as your selectedImageView property
UIImageView *pressedImageView = (UIImageView *)gestureRecognizer.view;
self.selectedImageView = pressedImageView;
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
[self savePhoto];
break;
default:
break;
}}
-(void)savePhoto{
UIGraphicsBeginImageContext(_selectedImageView.bounds.size);
[_selectedImageView drawRect:_selectedImageView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
NULL);
}
- (void) savedPhotoImage:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
NSString *message = @"This image has been saved to your Photos album";
if (error) {
message = [error localizedDescription];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
savePhoto是我相信搞亂了。
謝謝您的高級。
已更新: 所以這說,它保存它,但沒有出現。
-(void)savePhoto {
NSLog(@"TAPPED");
//Touch gestures below top bar should not make the page turn.
//EDITED Check for only Tap here instead.
CGPoint touchPoint = [touch locationInView:self.view];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
bool pageFlag = [userDefaults boolForKey:@"pageDirectionRTLFlag"];
NSLog(@"pageFlag tapbtnRight %d", pageFlag);
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
NSString *urlToSave = [webView stringByEvaluatingJavaScriptFromString:imgURL];
NSLog(@"urlToSave :%@",urlToSave);
NSURL * imageURL = [NSURL URLWithString:urlToSave];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
imageView.image = image;//imgView is the reference of UIImageView
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
NULL);
}
這只是拋出一個錯誤,使模擬器崩潰。 – ChrisOSX