Objective-C Google 网络相册 API - 尝试上传图像时出现奇怪的错误
我正在使用 API 的 Objective-C 版本。
我几乎完全复制了示例项目中的代码,但是我 每次尝试上传图片时都会出现此错误: “failedWithStatus:400 data:照片数据不能为空。”。
这是我的代码:
- (void)uploadToPicasa
{
// make a new entry for the photo
GDataEntryPhoto *newEntry = [GDataEntryPhoto photoEntry];
// set a title, description, and timestamp
[newEntry setTitleWithString:@"Title!"];
[newEntry setPhotoDescriptionWithString:@"Description!"];
[newEntry setTimestamp:[GDataPhotoTimestamp timestampWithDate:
[NSDate date]]];
// attach the NSData and set the MIME type for the photo
UIImage *earth_image = [UIImage imageNamed:@"earth.jpg"];
NSData *earth_data = UIImageJPEGRepresentation(earth_image, 1.0);
[newEntry setPhotoData:earth_data];
NSString *mimeType = @"image/jpeg";
[newEntry setPhotoMIMEType:mimeType];
// the slug is just the upload file's filename
[newEntry setUploadSlug:@"earth.jpg"];
// make service tickets call back into our upload progress
selector
GDataServiceGooglePhotos *service = [self googlePhotosService];
SEL progressSel =
@selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
[service setServiceUploadProgressSelector:progressSel];
// Get URL for Picasa
NSURL *uploadURL = [GDataServiceGooglePhotos
photoFeedURLForUserID:@"CORRECT_USERNAME"
albumID:nil
albumName:nil
photoID:nil
kind:nil
access:nil];
// insert the entry into the album feed
GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:newEntry
forFeedURL:uploadURL
delegate:self
didFinishSelector:@selector(addPhotoTicket:finishedWithEntry:error:)];
// no need for future tickets to monitor progress
[service setServiceUploadProgressSelector:nil];
}
// progress callback
- (void)ticket:(GDataServiceTicket *)ticket
hasDeliveredByteCount:(unsigned long long)numberOfBytesRead
ofTotalByteCount:(unsigned long long)dataLength {
NSLog([NSString stringWithFormat:@"%d", numberOfBytesRead /
dataLength]);
}
// photo add callback
- (void)addPhotoTicket:(GDataServiceTicket *)ticket
finishedWithEntry:(GDataEntryPhoto *)photoEntry
error:(NSError *)error {
if (error == nil) {
NSLog(@"SHOULD BE UPLOADED MAYBE");
} else {
NSLog(@"THERE WAS AN ERROR");
}
}
- (GDataServiceGooglePhotos *)googlePhotosService {
static GDataServiceGooglePhotos* service = nil;
if (!service) {
service = [[GDataServiceGooglePhotos alloc] init];
[service setShouldCacheResponseData:YES];
[service setServiceShouldFollowNextLinks:YES];
}
// update the username/password each time the service is requested
NSString *username = @"CORRECT_USERNAME";
NSString *password = @"CORRECT_PASSWORD";
if ([username length] && [password length]) {
[service setUserCredentialsWithUsername:username
password:password];
} else {
[service setUserCredentialsWithUsername:nil
password:nil];
}
return service;
}
我使用断点来确认 NSData 不为零,并且是 有问题的图像,所以我不确定什么“failedWithStatus:400 data:照片数据不能为空。”可能的意思是。请帮忙!
I'm using the Objective-C version of the API.
I've copied the code from the example project almost exactly, but am
getting this error every time I try to upload a picture:
"failedWithStatus:400 data:Photo data must not be empty.".
Here's my code:
- (void)uploadToPicasa
{
// make a new entry for the photo
GDataEntryPhoto *newEntry = [GDataEntryPhoto photoEntry];
// set a title, description, and timestamp
[newEntry setTitleWithString:@"Title!"];
[newEntry setPhotoDescriptionWithString:@"Description!"];
[newEntry setTimestamp:[GDataPhotoTimestamp timestampWithDate:
[NSDate date]]];
// attach the NSData and set the MIME type for the photo
UIImage *earth_image = [UIImage imageNamed:@"earth.jpg"];
NSData *earth_data = UIImageJPEGRepresentation(earth_image, 1.0);
[newEntry setPhotoData:earth_data];
NSString *mimeType = @"image/jpeg";
[newEntry setPhotoMIMEType:mimeType];
// the slug is just the upload file's filename
[newEntry setUploadSlug:@"earth.jpg"];
// make service tickets call back into our upload progress
selector
GDataServiceGooglePhotos *service = [self googlePhotosService];
SEL progressSel =
@selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
[service setServiceUploadProgressSelector:progressSel];
// Get URL for Picasa
NSURL *uploadURL = [GDataServiceGooglePhotos
photoFeedURLForUserID:@"CORRECT_USERNAME"
albumID:nil
albumName:nil
photoID:nil
kind:nil
access:nil];
// insert the entry into the album feed
GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:newEntry
forFeedURL:uploadURL
delegate:self
didFinishSelector:@selector(addPhotoTicket:finishedWithEntry:error:)];
// no need for future tickets to monitor progress
[service setServiceUploadProgressSelector:nil];
}
// progress callback
- (void)ticket:(GDataServiceTicket *)ticket
hasDeliveredByteCount:(unsigned long long)numberOfBytesRead
ofTotalByteCount:(unsigned long long)dataLength {
NSLog([NSString stringWithFormat:@"%d", numberOfBytesRead /
dataLength]);
}
// photo add callback
- (void)addPhotoTicket:(GDataServiceTicket *)ticket
finishedWithEntry:(GDataEntryPhoto *)photoEntry
error:(NSError *)error {
if (error == nil) {
NSLog(@"SHOULD BE UPLOADED MAYBE");
} else {
NSLog(@"THERE WAS AN ERROR");
}
}
- (GDataServiceGooglePhotos *)googlePhotosService {
static GDataServiceGooglePhotos* service = nil;
if (!service) {
service = [[GDataServiceGooglePhotos alloc] init];
[service setShouldCacheResponseData:YES];
[service setServiceShouldFollowNextLinks:YES];
}
// update the username/password each time the service is requested
NSString *username = @"CORRECT_USERNAME";
NSString *password = @"CORRECT_PASSWORD";
if ([username length] && [password length]) {
[service setUserCredentialsWithUsername:username
password:password];
} else {
[service setUserCredentialsWithUsername:nil
password:nil];
}
return service;
}
I used a breakpoint to confirm that NSData is not nil, and is the
image in question, so I'm not sure what "failedWithStatus:400
data:Photo data must not be empty." could mean. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用下面的方法:
// 获取相册 url 使用这个:
//
我在我的应用程序中尝试过并且它工作完美...
您也可以在 gdata api 中找到这个 url。
Instead of using below method:
// to fetch the album url use this one:
// where
I tried in my app and its working perfect...
You can find this url in gdata api also.